Perfect importing code not operating...

This is a discussion on Perfect importing code not operating... within the Windows Programming forums, part of the Platform Specific Boards category; This code fails, and I have no idea why, it's been really bugging me, PLEASE HELP!! main.cpp Code: #include <windows.h> ...

  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,211
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,211
    Which goes back to point 3 of what I said.....

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    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,893
    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,893
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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"
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    if in the header the function declared as extern "C" it will not be...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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?

Page 1 of 2 12 LastLast
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, 11:38 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03: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-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21