Converting simple C++ example to C, to use existing DLL

This is a discussion on Converting simple C++ example to C, to use existing DLL within the C Programming forums, part of the General Programming Boards category; I'm attempting to use an existing .dll file from within my C code. My coworker who created this .dll has ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    67

    Converting simple C++ example to C, to use existing DLL

    I'm attempting to use an existing .dll file from within my C code. My coworker who created this .dll has a simple example written in C++ that uses this file, which example code I'm able to use with no problems.

    However, I'm now attempting to "translate" his C++ code for use in my own C code, and so far have been getting long lists of compiler errors (understandably) because I'm attempting to use his C++ syntax within my C file. After searching online & on this forum, I haven't yet encountered the information I need, in order to convert his C++ code into a C syntax that will compile.

    This is his example code:
    Code:
    int a = 5, b = 2;
    
    typedef int (*sumPointer)(int a, int b); // Shorthand to make other lines shorter
    HINSTANCE dll;           // Pointer to the .dll
    sumPointer sum;         // Pointer to "sum" function within the .dll
    
    dll = LoadLibrary("RC.dll");
    sum = (sumPointer)GetProcAddress(dll, "sum");
    
    sum(a,b);

    Once I can get this simple example working within my C code, I'll be able to apply this same principle to the real .dll & function calls that I need to use. (I'm aware that the comment indicators above need to be changed to C format, for starters.)

    I appreciate any suggestions you have that will allow me to properly implement the above code in C. Thanks!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,507
    Was the DLL written in C or in C++?
    If it was written in C++; calling it from C may be impossible to very hard to do.

    Tim S.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    67

    Additional info: Compiler errors

    In case it's useful, I'll paste below the list of compiler errors I get, when I attempt to create a mex file using my current C code (with corrected comment indicators) and a related .lib file:

    controller_CMEXsfcn.c(230) : error C2065: 'HINSTANCE' : undeclared identifier
    controller_CMEXsfcn.c(230) : error C2146: syntax error : missing ';' before identifier 'dll'
    controller_CMEXsfcn.c(230) : error C2065: 'dll' : undeclared identifier
    controller_CMEXsfcn.c(231) : error C2275: 'sumPointer' : illegal use of this type as an expression
    controller_CMEXsfcn.c(229) : see declaration of 'sumPointer'
    controller_CMEXsfcn.c(231) : error C2146: syntax error : missing ';' before identifier 'sum'
    controller_CMEXsfcn.c(231) : error C2065: 'sum' : undeclared identifier
    controller_CMEXsfcn.c(233) : error C2065: 'dll' : undeclared identifier
    controller_CMEXsfcn.c(233) : warning C4013: 'LoadLibrary' undefined; assuming extern returning int
    controller_CMEXsfcn.c(234) : error C2065: 'sum' : undeclared identifier
    controller_CMEXsfcn.c(234) : warning C4013: 'GetProcAddress' undefined; assuming extern returning int
    controller_CMEXsfcn.c(234) : error C2065: 'dll' : undeclared identifier
    controller_CMEXsfcn.c(234) : warning C4047: '=' : 'int' differs in levels of indirection from 'sumPointer'
    controller_CMEXsfcn.c(236) : warning C4013: 'sum' undefined; assuming extern returning int

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    Thanks for bringing that issue to my attention, Tim. I'm almost certain that the .dll was written in C++. Do you know how I would determine, for certain, whether this poses an insurmountable problem for my planned code system?

    Update: I reviewed the source code from which the .dll was created, and indeed, it is C++...
    Last edited by CodeKate; 08-02-2011 at 10:28 AM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,507
    Quote Originally Posted by CodeKate View Post
    Thanks for bringing that issue to my attention, Tim. I'm almost certain that the .dll was written in C++. Do you know how I would determine, for certain, whether this poses an insurmountable problem for my planned code system?
    Look at the header file that should be used with the DLL; but, based on you code you are not using any header files.
    Edit: If his code example used extern "C" then likely it is a C DLL; otherwise likely an C++ DLL.

    The errors you are getting are from NOT using the header file that define HINSTANCE.

    Tim S.
    Last edited by stahta01; 08-02-2011 at 10:30 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First you need to #include <windows.h> for the LoadLibrary() call.
    Next you need to #include the header for the dll

    C++ DLLs can usually be accessed by C programs, but using classes in C is a major pain in the backside since you have to define your own VTBL for class functions (C++ does this invisibly) and call them by function pointers... As a general rule if the DLL contains only functions you should be able to use it. But when it includes Classes, you need to be a well established masochist to get it working.

    In the long run it would probably be easier to use his work as a model and then re-write the DLL...

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    Ok, I reviewed the source code specifically for my DLL, and it *does* start with 'extern "C" '. I should mention that both the example code/.dll as well as the real versions of those files, were specifically created for my use with my *C* code, so I believe that they all *should* be compatible with C.

    That having been established, are there any suggestions about how the few lines of C++ code in my OP might be able to be translated into C? I appreciate all input.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    Thanks for your input, Tater. You've given me a lot to consider.
    Last edited by CodeKate; 08-02-2011 at 10:46 AM.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,507
    Quote Originally Posted by CodeKate View Post
    That having been established, are there any suggestions about how the few lines of C++ code in my OP might be able to be translated into C? I appreciate all input.
    I see nothing in your code you posted that is NOT valid C code; but, you still need to include the needed header files!

    Tim S.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    Update: just by including the <windows.h> and .dll header call (duh), as Tater suggested, the code is now compiling successfully with no warnings. This isn't necessarily the end of my issues, but is a huge step forward. Thanks, Tater (and Tim)!

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by CodeKate View Post
    Update: just by including the <windows.h> and .dll header call (duh), as Tater suggested, the code is now compiling successfully with no warnings. This isn't necessarily the end of my issues, but is a huge step forward. Thanks, Tater (and Tim)!
    Well... now you know why we use header (.h) files....

    Good luck with the rest of the project.

  12. #12
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    Well... now you know why we use header (.h) files....
    yeah, thanks. It's not that I was unfamiliar with header files, but just that there were already a number of them included in my code, and I wasn't sure if others were missing...

    Good luck with the rest of the project.
    Thanks a lot!

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by CodeKate View Post
    yeah, thanks. It's not that I was unfamiliar with header files, but just that there were already a number of them included in my code, and I wasn't sure if others were missing...
    Thanks a lot!
    "I use 10 of them, what do you want?" doesn't work here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Program For Converting Change to Dollars
    By Will.I.Am in forum C Programming
    Replies: 12
    Last Post: 02-01-2009, 10:20 PM
  2. rand() existing variables?
    By ignotus in forum C++ Programming
    Replies: 7
    Last Post: 01-01-2009, 09:20 PM
  3. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  4. An application that starts itself after existing
    By dit6a9 in forum Windows Programming
    Replies: 6
    Last Post: 04-17-2005, 03:33 PM
  5. WinSock // Existing Games
    By CPP-Null in forum C++ Programming
    Replies: 8
    Last Post: 05-24-2003, 12:23 PM

Tags for this Thread


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