Results 1 to 19 of 19

Thread: Calling Function In Exe

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341

    Calling Function In Exe

    hey,

    I know that you can use LoadLibrary() and GetProcAddress() to load a function up from a .dll.

    I have tried this but with using an exe instead although it doesnt work.

    Is it possible ?

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    You can. You just export your functions from the EXE the same way you would with a DLL.

    Z.

  3. #3
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Zaei -

    I think he wants oemthing like sHell in VB -

    Use the ShellExecute() api.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    okay Zaei, i tried that although it says this:

    Debug/Project2.exe : warning LNK4086: entrypoint "_mainCRTStartup" is not __stdcall with 12 bytes of arguments; image may not run

    it compiles (as its only a warning) although it doesnt run properly.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Do you use a custom entry point? Maybe have a corrupted CRT?

    This is very strange...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    I changed it to a normal windows app (non Console) and got this error:

    Creating library Debug/Project2.lib and object Debug/Project2.exp
    Debug/Project2.exe : warning LNK4086: entrypoint "_WinMainCRTStartup" is not __stdcall with 12 bytes of arguments; image may not run

    The code in main.cpp

    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	return 0;
    }
    
    void __declspec(dllexport) Hello (void)
    {
    	MessageBox(NULL, "Hello", "Project2", MB_OK);
    }
    The code in main.def

    Code:
    LIBRARY "Project2.exe"
    
    EXPORTS
    	Hello
    I pardon any crap C++ coding on mybehalf

  7. #7
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Alrighty, here is the deal. The compiler is reading your .def file, and exporting all of the functions in there. Fine. Unfortunatly, it does NOT export the exe entry point, thus, well, no entry point. I tried adding it to the def file, but it complains about an unresolved external symbol, and such. What you acn do instad is use the dependency viewer on your exe (depends.exe, comes with VS), and find out the mangled names of the functions you export, and use THOSE names when calling LoadLibrary().

    Z.

  8. #8
    Junior Member
    Join Date
    Apr 2002
    Posts
    20
    As far as I know, if you declare a function with __declspec(dllexport) keyword, you don`t need to write the function`s name in a *.def file, too.
    Removing the main.def file may solve the problem.
    Last edited by Techno-Logic; Sep 11th, 2002 at 09:37 AM.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But then you have problems using the import library with any other compiler than with which it was created.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    This problem is now solved. i was wanting to call a function in a vb exe but this wasnt working so i tried a C++ one. neither worked and i knew because VB cant export a function. im now using different methods.

    Thanks again for help.

  11. #11
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    CornedBee is correct. Without the .def file, the function will export, but it will shows up as its mangled name. The def file exports the function name UNmangled, and thus makes it a bit easier to use. If you are using implicit linking, this is a moot point, since only the compiler cares what the functions are called, but if you need to call the function from another language, using the def file makes things a bit easier.

    Z.

  12. #12
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Hi CornedBee and Zaei, Just a quick question. Can i have both the definition file (.def) and the export the function with __declspec(dllexport) syntax together?

    So, my exported function can be access either through implicit or explicit link.

    regards,
    Chris

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The __declspec(dllexport) is not necessary when you have a def file. It was invented by MS to make creating dlls that are only linked to implicitly easier. And to allow the exporting of classes.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Makes me wonder if you could use a C style member function call in VB with an exported class from a DLL... Something to try sometime =).

    Z.

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can. Look up the mangled name, create a Type in VB that matches the classes internal data structure, have the first param of the function be ByRef this As ClassData and off you go.
    The tricky thing of course is calling the constructor and destructor.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    That is what I figured =). Goodness it is nice to be able to figure this kind of stuff out =).

    Z.

  17. #17
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    yup, but can i have both together? 'coz i'm writing an SDK for others party to develop. If i have both together, then my user can either use my dll in implicit or explicit link.

    regards,

    Originally posted by CornedBee
    The __declspec(dllexport) is not necessary when you have a def file. It was invented by MS to make creating dlls that are only linked to implicitly easier. And to allow the exporting of classes.

  18. #18
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Yes, you can use both together.

    Z.

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But it shouldn't be necessary as implicit linking should work with def files too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width