20

I have .lib file with its header (.h) file. This file have a few functions that need to be used in C# application.

After googling I found that I need to create a dynamic DLL from this static library and call this dynamic DLL from C# code using interop.

  1. I have created a win32 project and selected type DLL.

  2. Included header file and added .lib to additional dependencies.

    I am able to see the functions defined in the static library (when I press ctrl + space).

As a total newbie I do not know how I can export the function, which is, in .lib with following signature:

void testfun( char* inp_buff, unsigned short* inp_len, char* buffer_decomp,unsigned *output_len,unsigned short *errorCode)

I want same signature in my dynamic DLL with a different name.

What to write in header file and .cpp file?

Community
  • 1
  • 1
Manjoor
  • 4,091
  • 10
  • 43
  • 67

6 Answers6

10

If you can recompile your lib, just add __declspec(dllexport) to the signatures of all of the functions you want to be exported.

void __declspec(dllexport) testfun( char* inp_buff, unsigned short* inp_len, char* buffer_decomp,unsigned *output_len,unsigned short *errorCode)

If you can't do that, then you can export them by writing a .def file instead. Using def files you can even change the name of a function as it is exported. http://msdn.microsoft.com/en-us/library/28d6s79h.aspx

---- contents of mylib.def ----

LIBRARY

EXPORTS
   testfun
   newname=testfun2

Then when you link the dll, include mylib.def

link /dll /machine:x86 /def:mylib.def  mylib.lib

Edit2:

note that pinvoke assumes that the functions you import will have _stdcall calling convention unless you say otherwise. So you might need to do this as well, in your C# code.

[DllImport("mylib.dll", CallingConvention=CallingConvention.Cdecl)]

Or, you could change your C++ code to be __stdcall

void __declspec(dllexport) __stdcall testfun( char* inp_buff, ...
John Knoeller
  • 33,512
  • 4
  • 61
  • 92
  • I have created the dll. but when i call it from C#, it said "Unable to find an entry point named 'testfun2' in DLL 'demo.dll'" When i see list of exported functions using DLL export Viewer It shows the function name as "?testfun2@@YAXPADPAG0PAI@Z" What would be the problem?? – Manjoor Mar 05 '10 at 07:30
  • @Manjoor: it might be. did you use a def file or _declspec? If you used _declspec, then you probably just need to fix your C# dllImport statements. It would be helpful to see some code. – John Knoeller Mar 05 '10 at 08:18
4

This is what you can do

  1. Add the following code to you .H file. rename "MYPROJECT" to your project name

    #ifdef MYPROJECT_EXPORTS
    #define MYPROJECT_API __declspec(dllexport)
    #else
    #define MYPROJECT_API _declspec(dllimport)
    #endif
    
  2. Go to Properties->C++->Preprocessor and Add the defenition - MYPROJECT_EXPORTS

  3. Add MYPROJECT_API to all the functions you want the dll to expose eg:

    MYPROJECT_API void Test();
    
  4. Go to Project properties General -> Configuration Type change it to Dynamic Dll

You are done

vladr
  • 65,483
  • 18
  • 129
  • 130
SysAdmin
  • 5,455
  • 8
  • 33
  • 34
  • I have done it as you guys told me to do. dll created. but i am not able to call it from my c# application. see the comment above posted by me. – Manjoor Mar 05 '10 at 08:00
  • Hi..the function name you see is wierd because of c++ name mangling try putting : extern "C" in front of your function and recompile – SysAdmin Mar 05 '10 at 08:35
  • as you said i have added extern "C" before function in my header file. but no luck (See the code below) – Manjoor Mar 05 '10 at 08:41
3

Create new Dll project using Visual Studio Application Wizard, and check "Exports Symbols" in one of the Wizard steps. It creates sample Dll which exports class, function and variable. You can learn from this sample, how to do this. Generally, every exported function is declared as __declspec(dllexport). In a client project it is declared as __declspec(dllimport). Dll code uses the constant which is defiled as __declspec(dllexport) inside of Dll project, and __declspec(dllimport) in any other place.

Alex F
  • 42,307
  • 41
  • 144
  • 212
1

there are two versions of LIB can be generated, the fist is the dynamic lib, (source file + header+ dynamic lib) --> to access the DLL

or static lib=(dynamic lib+DLL) --> (Source file+header) --> to access the DLL.

if you have the Dynamic Lib > there is no way to create the DLL (you cannot get something from nothing), dynamic lib is just an interface,

but if you have the Static Lib then there is no need to DLL to access it is functions.

Bashar
  • 31
  • 4
0

Take a look at my answer to this question for a possible solution. Almost positive this will work for you...

In short: Enable the "Use Library Dependency Inputs" option in your Linker settings. If set to "true", it will force linking ALL symbols & code declared in every LIB specified as input to the project.

Community
  • 1
  • 1
James Hugard
  • 3,232
  • 1
  • 25
  • 36
0

The issue here is not how the code is decorated, it's the extra step of creating a static library that contains all of the entry points, and trying to build the dll out of that.

If you go with the __delcspec approach and build the static library first, then try to link to it when building a DLL, you'll have to solve the dead-code stripping problem.

When you link, the obj srcs are used to find all of the decorated exports and dependencies are resolved, everything else is stripped. If you have no DLL src, so no obj files (except maybe a dll main), all of the code in the lib you want to export will be stripped (regardless of the attributes).

So, you either have to:

  1. Tell the linker not to strip unused code, which is probably going to give you a lot of stuff you don't want.
  2. Use a def file to expose the exports manually
  3. Link the dll against the obj files used to create the lib instead of linking to the lib directly.
  4. Or maybe create dummy code that references the functions you want to export from something you are exporting.
johnb003
  • 1,821
  • 16
  • 30