Unmanaged C++ .lib to C#

M

Marty

Hi,

I want to wrap an unmanaged c++ lib file (mylib.lib) to use it in a
VS.NET 2003 C# project.

I use the DLLImport but it can't see the lib file. Is it because it is
a lib file instead of a dll?

[DllImport("c:\\myLib.lib")]
public static extern string myFunction(string strTest);

Thank you!
Marty
 
G

Guest

Unfortunately this will not work as you would like it as DLLImport is used
for accessing functions within it.

You pretty much have two options to make this work... use managed c++ to
expose a managed lass, or build an unmanaged c++ dll that acts as a wrapper
to the lib. I would highly recommend looking into the 2nd method myself.

Brendan
 
M

Marty

Hi Brendan,

Thanks for your help.

If I make a wrapper, you said I should do it in unmanaged c++ ? Will it
be exposed to C#? I was thinking to make it in managed C++.

Thanks,
Marty

Brendan said:
Unfortunately this will not work as you would like it as DLLImport is used
for accessing functions within it.

You pretty much have two options to make this work... use managed c++ to
expose a managed lass, or build an unmanaged c++ dll that acts as a wrapper
to the lib. I would highly recommend looking into the 2nd method myself.

Brendan


:

Hi,

I want to wrap an unmanaged c++ lib file (mylib.lib) to use it in a
VS.NET 2003 C# project.

I use the DLLImport but it can't see the lib file. Is it because it is
a lib file instead of a dll?

[DllImport("c:\\myLib.lib")]
public static extern string myFunction(string strTest);

Thank you!
Marty
 
A

at

Yes.

A .lib describes what is in the dll enabling the linker to create the proper
glue for call into the dll. If you have the dll as well as the exported sigs
you should have no real problem, you would not even need the .lib then.
 
S

Suriyanto Lee

Marty,

Yes, that's the reason. lib is only usable from C++. You will have to
compile it into dll and expose the functions using DllExport before you
can import it into other environment.

Suriyanto
 
A

at

Is a .lib file all you have got? If so, then it is a nogo. What do you have?

If you have the C++ source as well and are able to compile/link it into a
dll then hope can be found in using DllImport specs that give a sig that can
be used from .Net. Then you should focus on that. Tip: try to make a very
simple dll with a single exported function and see if you are able to call
that function from .Net.
 
M

Marty

Well I have two headers files, that contain constants and functions
signatures, and I have the .lib file.

The headers files uses data types such as WORD, CString, LPCSTR, all
part of unmanaged code I guess.

I tried to include these two header file in a VC++.NET DLL project. It
couldn't compile because of those datatype.

Is there any compiler switch I should specifically set?

Thanks a lot!
Marty
 
R

Ryan Chavez

Create a new Win32 C++ Project as apposed to a .NET project.
Enter your Project name and in the new App Wizard popup,
select:
* Application type: DLL
* Additional options: Export symbols
Click on Finish and you should have a somewhat empty project that will
compile to a DLL.
Create your functions that wrap the header files that you have (don't forget
to include them in your project as well as the lib).
Next, add a new item of type "Module-Definition File (.def)" and add the
following lines:
EXPORTS
WrappedFunction_1 @1
And just add the functions names in the order that your create them.
After you're done and compiled the DLL, simply use the appropriate PInvoke
syntax
and Marshaling to access them.

Hope that helps.

Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top