[Q] How to use a static Lib in C#

A

Andre

Hi,

I have a static Lib and I want to use it in C#.

So, I tried to wrap these functions in a DLL (not in a class) and use
[Dllimport()]. But this doesn't work.

Is it possible to wrap this in a DLL not using a class and to use it
with DllImport like using kernel32.dll and how?

Thanks
André Betz
http://www.andrebetz.de
 
N

Nicholas Paldino [.NET/C# MVP]

Andre,

That is exactly what you have to do. You have to create a dll which
will have the same profile (in terms of function exports) that the lib has,
and then bind to that in your .NET code.

Hope this helps.
 
A

Andre

Hi,

I have tried this following:

I created with MS Visual Studio a Win32 DLL with export and set in
project Settings "use managed extension" to yes, because I want an
unmanaged cpp-Wrapper Class. Further I linked my static lib to this
project, created a class K and inside a memeber func K::F which call a
static lib function. So now I comiled it and put it into references of
my C# project. In the object browser I see the class but I don't see
any member fnctions.

- So is something wrong, can I call K::F?

- Can I also call K::F with DLLimport and how?

Thanks
André Betz
http://www.andrebetz.de
 
R

Richard Grimes [MVP]

Andre said:
I created with MS Visual Studio a Win32 DLL with export and set in
project Settings "use managed extension" to yes, because I want an
unmanaged cpp-Wrapper Class.

If the C# will use [DllImport] then you do not need to make the DLL managed.

If you make it managed then don't use [DllIimport] instead create a public
class and provide static wrapper methods for the methods in the unmanaged
static library.

Note that [DllImport] can only work if the types are marshalable. That is
the basic primitive .NET types (and string). If the methods in the static
library have more complex parameters (like structs or pointers) then you'll
be better off creating managed wrappers to those structs.
Further I linked my static lib to this
project, created a class K and inside a memeber func K::F which call a
static lib function. So now I comiled it and put it into references of
my C# project. In the object browser I see the class but I don't see
any member fnctions.

Did you make K::F a public method? What are the parameters to this method -
are they primitive types?
- So is something wrong, can I call K::F?

- Can I also call K::F with DLLimport and how?

If you compile the DLL as a managed DLL then you should make the class a
managed class (__gc) and public, and make the methods public. Then call it
directly, you do not need [DllImport]

Richard
 
G

Guest

This sounds like what I need to do for a project I am working on but I'm
pretty much a newbie to the whole interop process and my C programming skills
are rusty.

Is there an example of doing this somewhere? All I've been able to dig up
are ways to wrap DLL functions.

Thanks,
Carl

Richard Grimes said:
Andre said:
I created with MS Visual Studio a Win32 DLL with export and set in
project Settings "use managed extension" to yes, because I want an
unmanaged cpp-Wrapper Class.

If the C# will use [DllImport] then you do not need to make the DLL managed.

If you make it managed then don't use [DllIimport] instead create a public
class and provide static wrapper methods for the methods in the unmanaged
static library.

Note that [DllImport] can only work if the types are marshalable. That is
the basic primitive .NET types (and string). If the methods in the static
library have more complex parameters (like structs or pointers) then you'll
be better off creating managed wrappers to those structs.
Further I linked my static lib to this
project, created a class K and inside a memeber func K::F which call a
static lib function. So now I comiled it and put it into references of
my C# project. In the object browser I see the class but I don't see
any member fnctions.

Did you make K::F a public method? What are the parameters to this method -
are they primitive types?
- So is something wrong, can I call K::F?

- Can I also call K::F with DLLimport and how?

If you compile the DLL as a managed DLL then you should make the class a
managed class (__gc) and public, and make the methods public. Then call it
directly, you do not need [DllImport]

Richard
--
..NET training, development, consulting and mentoring
my email (e-mail address removed) is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/
 

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