Thread: Perfect importing code not operating...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Perfect importing code not operating...

    This code fails, and I have no idea why, it's been really bugging me, PLEASE HELP!!

    main.cpp
    Code:
    #include <windows.h>
     
    typedef int (WINAPI *CommProt)(int);
    #define ReportError(x) MessageBox(0, TEXT(x), TEXT("Error"), MB_OK | MB_ICONERROR);
    
    int WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShow)
    {
       HMODULE hDLL = LoadLibrary("appext.dll");
       if(hDLL == NULL)
          ReportError("Load of DLL failed.");
       CommProt CommFunc = (CommProt)GetProcAddress(hDLL,"Comm"));
       // This is where the error happens, the GetProcAddress() call fails.
       if(CommFunc == NULL)
          ReportError("Capture of function failed.");
       Comm(5); // This is where the program crashes.
       FreeLibrary(hDLL);
       hDLL = NULL;
       return 0;
    }
    appext.cpp
    Code:
    #include <windows.h>
     
    int Comm(int nVar)
    {
       char szDom[MAX_PATH];
       wsprintf(szDom, TEXT("Comm var &#37;d."), nVar);
       MessageBox(0, szDom, TEXT("Comm"), MB_OK | MB_ICONINFORMATION);
       return 0;
    }
     
    BOOL APIENTRY DllMain(HANDLE hMod, DWORD ulCall, LPVOID lpRes)
    {
       return TRUE;
    }
    -Quaytrix
    Last edited by Queatrix; 06-22-2007 at 05:59 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    1. Dumping the code here and telling us it fails is stupid. We know it fails because you wouldn't be here asking for help otherwise. Try to give a little more information. At least you told us where the problem seems to be and what is occurring.
    2. You are not checking if the DLL was properly loaded. It could be failing to load.
    3. You are not checking if the function's location in said DLL was found. It could be failing to find such a function, especially since you define it as:

      Code:
      int Comm(int nVar);
      But you are declaring your function pointer to be of type:

      Code:
      int (WINAPI *CommProt)(int)
      Do you know the difference between C's default calling convention and the WINAPI? Do you know what happens if you mix them up?


    That's for starters.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char sz[MAX_PATH];
    > wsprintf(sz, "Comm var &#37;d.", nVar);
    > MessageBox(0, sz, "Comm()", MB_OK | MB_ICONINFORMATION);
    Use the TCHAR and TEXT macros to ensure UNCODE consistency throughout your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Okay, I have provided all the info I can think would be usefull. The program does get the dll sucessfuly, but can't capture the function.

    And I used the macro where I have written text now too.

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    is it possible that the function is not being found because you are not using the correct calling conventions?

    http://msdn2.microsoft.com/en-us/lib...z2(VS.71).aspx

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Which goes back to point 3 of what I said.....

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Does the DLL contain C++ code? Then you have to contend with name-mangling issues.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yep, it's C++ code, and it's almost surely a name mangling issue. You should provide a .def file for the library.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Okay, I fixed the function and is using dllexport now too, but it still isn't working. I'm wondering if this isn't a compilation problem, does it compile and run fine for you?
    main.cpp
    Code:
    #include <windows.h>
    
    typedef __declspec(dllexport) int (WINAPI *CommProt)(int);
    #define ReportError(x) MessageBox(0, TEXT(x), TEXT("Error"), MB_OK | MB_ICONERROR);
    
    int WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShow)
    {
       HMODULE hDLL = LoadLibrary("appext.dll");
       if(hDLL == NULL)
          ReportError("Load of DLL failed.");
       CommProt CommFunc = (CommProt)GetProcAddress(hDLL,"Comm"));
       // This is where the error happens, the GetProcAddress() call fails.
       if(CommFunc == NULL)
          ReportError("Capture of function failed.");
       Comm(5); // This is where the program crashes.
       FreeLibrary(hDLL);
       hDLL = NULL;
       return 0;
    }
    appext.cpp
    Code:
    #include <windows.h>
    
    __declspec(dllexport) int WINAPI Comm(int nVar)
    {
       char szDom[MAX_PATH];
       wsprintf(szDom, TEXT("Comm var &#37;d."), nVar);
       MessageBox(0, szDom, TEXT("Comm"), MB_OK | MB_ICONINFORMATION);
       return 0;
    }
     
    BOOL APIENTRY DllMain(HANDLE hMod, DWORD ulCall, LPVOID lpRes)
    {
       return TRUE;
    }
    -Quaytrix

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Have you written a .def file yet?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    dumpbin <dllname> /EXPORTS

    or some dllExplorer will show you the real names expoter by your dll (without def file)

    I suppose it is something like "Comm@4"
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Even more complicated. I expect it to be a C++ mangled name. Something like _Comm??i?z
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if in the header the function declared as extern "C" it will not be...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But there is no header file.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> Bee: Have you written a .def file yet?

    No, the compiler does that for me.

    >> vart: I suppose it is something like "Comm@4"
    >> Bee: I expect it to be a C++ mangled name.

    Does this mean that I can replace my string in GetProcAddress() with the one from the .def file, and then it'll work?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM