Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Issue with GetProcAddress (Resolved)

Issue with GetProcAddress (Resolved)

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++csharplearning
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel Gow
    wrote on last edited by
    #1

    Hello! I'll preface this by saying I'm a complete novice with C++; the bulk of my programming has been in VB 5/6 and VB .Net, but I have an issue with an SDK that is designed for C++, and I need it to work with VB.Net. My issue is this; two of the functions in the DLL supplied with the SDK use a structure that has a datatype in it that is a Union (unions, of course, aren't that well supported in VB .Net). So, I decided the best route was to create my own C++ DLL that would handle the two functions that need to use this Union datatype, meaning all I would need to do is supply an input parameter (an integer in my case) and recieve and output parameter back (another integer). Calling the functions from VB .Net is easy, but that's not my problem. I've written the C++ DLL basically by copying and pasting from the C++ header files supplied with the SDK, as that made the most sense given my lack of C++ knowledge. I have a header file that has all of the datatype definitions, an additional header file for the structure and union definitions, and then a ".cpp" file for the actual function call: void ReturnTheDataINeed(cdHandle hEnum, cdHandle &hnd) { HINSTANCE hLib; cdError err; cdHEnum hEnumDevice; cdSourceInfo* pSourceInfo; cdSourceInfo SSrcInfo; cdHEnum hNext; cdEnumDeviceNext NextDevice; hLib = LoadLibrary("CDSDK.dll"); NextDevice = (cdEnumDeviceNext)GetProcAddress(hLib,"cdEnumDeviceNext"); pSourceInfo= new cdSourceInfo; err = NextDevice(hEnum, pSourceInfo); //err = cdOpenSource( &SSrcInfo, &hEnumDevice); FreeLibrary(hLib); hnd = hEnumDevice; }; These are the type definitions (also copied from the SDK)... typedef unsigned long cdError; typedef unsigned long cdHEnum; - cdSourceInfo is a structure that holds data about the device, and I can supply the definition if it will help. cdEnumDeviceNext is a function pointer as follows...this is copied out of the SDK's header files under 'function pointers'. typedef cdError cdSTDCALL cdEnumDeviceNext( cdHEnum hEnum, cdSourceInfo* pSourceInfo ); cdSTDCALL is defined as this: #define cdSTDCALL __stdcall And finally, I have the second function (cdOpenSource) commented out because...this dll does not build. When I attempt to build it, this line: NextDevice = (cdEnumDeviceNext)GetProcAddress(hLib,"cdEnumDeviceNext"); ...throws the error "C2066: cast to function type is illegal" and what I pres

    A 1 Reply Last reply
    0
    • D Daniel Gow

      Hello! I'll preface this by saying I'm a complete novice with C++; the bulk of my programming has been in VB 5/6 and VB .Net, but I have an issue with an SDK that is designed for C++, and I need it to work with VB.Net. My issue is this; two of the functions in the DLL supplied with the SDK use a structure that has a datatype in it that is a Union (unions, of course, aren't that well supported in VB .Net). So, I decided the best route was to create my own C++ DLL that would handle the two functions that need to use this Union datatype, meaning all I would need to do is supply an input parameter (an integer in my case) and recieve and output parameter back (another integer). Calling the functions from VB .Net is easy, but that's not my problem. I've written the C++ DLL basically by copying and pasting from the C++ header files supplied with the SDK, as that made the most sense given my lack of C++ knowledge. I have a header file that has all of the datatype definitions, an additional header file for the structure and union definitions, and then a ".cpp" file for the actual function call: void ReturnTheDataINeed(cdHandle hEnum, cdHandle &hnd) { HINSTANCE hLib; cdError err; cdHEnum hEnumDevice; cdSourceInfo* pSourceInfo; cdSourceInfo SSrcInfo; cdHEnum hNext; cdEnumDeviceNext NextDevice; hLib = LoadLibrary("CDSDK.dll"); NextDevice = (cdEnumDeviceNext)GetProcAddress(hLib,"cdEnumDeviceNext"); pSourceInfo= new cdSourceInfo; err = NextDevice(hEnum, pSourceInfo); //err = cdOpenSource( &SSrcInfo, &hEnumDevice); FreeLibrary(hLib); hnd = hEnumDevice; }; These are the type definitions (also copied from the SDK)... typedef unsigned long cdError; typedef unsigned long cdHEnum; - cdSourceInfo is a structure that holds data about the device, and I can supply the definition if it will help. cdEnumDeviceNext is a function pointer as follows...this is copied out of the SDK's header files under 'function pointers'. typedef cdError cdSTDCALL cdEnumDeviceNext( cdHEnum hEnum, cdSourceInfo* pSourceInfo ); cdSTDCALL is defined as this: #define cdSTDCALL __stdcall And finally, I have the second function (cdOpenSource) commented out because...this dll does not build. When I attempt to build it, this line: NextDevice = (cdEnumDeviceNext)GetProcAddress(hLib,"cdEnumDeviceNext"); ...throws the error "C2066: cast to function type is illegal" and what I pres

      A Offline
      A Offline
      A T I F
      wrote on last edited by
      #2

      GetProcAddress return the pointer to function of the call you are trying to make and in your case it will be returning address to pointer of cdEnumDeviceNext. I don;t know about what was being done there but you should have a declaration of pointer to function of 'cdEnumDeviceNext' something as follows typedef cdError (CALLBACK* LPFNcdEnumDeviceNext)(cdHEnum, pSourceInfo); and then you go like this.. hLib = LoadLibrary("CDSDK.dll"); LPFNcdEnumDeviceNext NextDevice = NULL; NextDevice = (LPFNcdEnumDeviceNext)GetProcAddress(hLib,"cdEnumDeviceNext"); // instead of NextDevice = (cdEnumDeviceNext)GetProcAddress(hLib,"cdEnumDeviceNext"); pSourceInfo= new cdSourceInfo; err = NextDevice(hEnum, pSourceInfo); //err = cdOpenSource( &SSrcInfo, &hEnumDevice); FreeLibrary(hLib); I hope you understand and that I have not made it more difficult for you...

      D 1 Reply Last reply
      0
      • A A T I F

        GetProcAddress return the pointer to function of the call you are trying to make and in your case it will be returning address to pointer of cdEnumDeviceNext. I don;t know about what was being done there but you should have a declaration of pointer to function of 'cdEnumDeviceNext' something as follows typedef cdError (CALLBACK* LPFNcdEnumDeviceNext)(cdHEnum, pSourceInfo); and then you go like this.. hLib = LoadLibrary("CDSDK.dll"); LPFNcdEnumDeviceNext NextDevice = NULL; NextDevice = (LPFNcdEnumDeviceNext)GetProcAddress(hLib,"cdEnumDeviceNext"); // instead of NextDevice = (cdEnumDeviceNext)GetProcAddress(hLib,"cdEnumDeviceNext"); pSourceInfo= new cdSourceInfo; err = NextDevice(hEnum, pSourceInfo); //err = cdOpenSource( &SSrcInfo, &hEnumDevice); FreeLibrary(hLib); I hope you understand and that I have not made it more difficult for you...

        D Offline
        D Offline
        Daniel Gow
        wrote on last edited by
        #3

        Thanks for your reply! I'm sure I never would have figured that out on my own...and I understand it vaguely enough for it to make sense to me - which is enough. It works great too, builds properly and all that and I added a little bit of error handling (such as it is) to the mix. One problem I'm having though is that my VB app can't find the function in the DLL. Is there a special way I need to declare the function so that it's what I'd call "Public" in a VB .Net sense, or visible beyond the scope of the inner workings of the DLL? At the moment the function starts as... cdError ReturnCamera(cdHandle hEnum, cdHandle &hnd) - cdError is just another unsigned long. Thanks again!

        A 1 Reply Last reply
        0
        • D Daniel Gow

          Thanks for your reply! I'm sure I never would have figured that out on my own...and I understand it vaguely enough for it to make sense to me - which is enough. It works great too, builds properly and all that and I added a little bit of error handling (such as it is) to the mix. One problem I'm having though is that my VB app can't find the function in the DLL. Is there a special way I need to declare the function so that it's what I'd call "Public" in a VB .Net sense, or visible beyond the scope of the inner workings of the DLL? At the moment the function starts as... cdError ReturnCamera(cdHandle hEnum, cdHandle &hnd) - cdError is just another unsigned long. Thanks again!

          A Offline
          A Offline
          A T I F
          wrote on last edited by
          #4

          Any function to be exported from a DLL should be preceeded by some declaraion that shows that this is an exported function. So you can try to declare the function as extern "C" __declspec(dllexport) cdError __cdecl ReturnCamera(cdHandle hEnum, cdHandle &hnd) __declspec(dllexport) shows that this is a function that dll exports and all other define C type convention for export. with the above declaration I have called this function from C# .NET so I think this should work with VB .NET

          D 1 Reply Last reply
          0
          • A A T I F

            Any function to be exported from a DLL should be preceeded by some declaraion that shows that this is an exported function. So you can try to declare the function as extern "C" __declspec(dllexport) cdError __cdecl ReturnCamera(cdHandle hEnum, cdHandle &hnd) __declspec(dllexport) shows that this is a function that dll exports and all other define C type convention for export. with the above declaration I have called this function from C# .NET so I think this should work with VB .NET

            D Offline
            D Offline
            Daniel Gow
            wrote on last edited by
            #5

            Thanks so much for your reply! I unfortunately can't say that my DLL is working, but the problem I have now is one in relation to the SDK, not with C++ or the help you offered. The 'extern "C" __declspec..." line did the trick fine, and I'm starting to get the hang of this! Thanks again.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups