473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
Nov 16 '05 #1
4 16294
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andre" <sk******@gmx.de> wrote in message
news:be*************************@posting.google.co m...
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

Nov 16 '05 #2
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
Nov 16 '05 #3
Andre wrote:

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 ev******@zicf.bet is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/
Nov 16 '05 #4
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 [MVP]" wrote:
Andre wrote:

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 ev******@zicf.bet is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.