0

Suppose we have: mysolution.sln in MS VS 2008, under which say we have 2 projects:

1) project1.vcproj - Here we have project1_file1.cpp, in which we have function defined:

 doSomething(){ ... } 

2) project2.vcproj - Say, here we have project2_file1.h and project2_file1.c. What I am trying to achieve in project2_file1.c is something like this, where:

myJumpTable_t myJumpTable = { doSomething }; 

I understand that we must have a struct defined somewhere:

 typedef struct _myJumpTable_t { void (*doSomething)(); }myJumpTable_t; 

But I am not sure, where this must be declared? I am also aware that we must be using __declspec(dllimport), again I am not sure how to use this. Please help me with this. I am stuck with this issue from many days.

Thank you so much in advance.

2 Answers 2

1

Firstly, by default, no functions are exported from a DLL. You must manually export them either with a .map file, or __declspec(dllexport).

Anything that you are importing from the DLL must be defined in more or less the same way it would be if it was just a normal part of the program, except the declaration must begin with __declspec(dllimport). This would typically be done in a header file which is part of the DLL project file, and would select __declspec(dllexport) when compiling the DLL (unless you are using a .map file), and __declspec(dllimport) when compiling the EXE

In the interest of keeping this simple, I will not stick to that advice.

project1_file1.cpp

//This is exporting the function from the DLL so that it can be found __declspec(dllexport) void doSomething() { ... } 

project2_file1.h

//This is where we declare the function so our code knows what it is __declspec(dllimport) void doSomething(); typedef struct _myJumpTable_t { void (*doSomething)(); }myJumpTable_t; 

project2_file1.c

myJumpTable_t myJumpTable = { &doSomething //& symbol here is optional, but good practice }; 

This follows your original code, but note that you can call doSomething directly in the EXE once declared. There is no need for the table, unless your other code uses that.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for your response. I followed your code, however I am getting a warning message treated as a warning:error C2220: warning treated as error - no 'object' file generated warning C4232: nonstandard extension used : 'doSomething' : address of dllimport 'doSomething' is not static, identity not guaranteed
I am getting these errors because I am trying to export a C++ function to C. Please help me fix this. Thanks.
@user1128265 you can add extern "C" to the beginning of the function declaration in the C++ code. This will fix up your linkage. extern "C" __declspec(dllimport) void doSomething();
0

Are you building DLL and application that will use this DLL or both projects are just executable modules and you are trying to reuse the code?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.