how to use dl file in C# ,and show a example to it ,thanks!!

  • Thread starter Thread starter jebelsea
  • Start date Start date
Do you mean "how to use a dll file from C#"? If this is what you
are asking, then I recommend you read about p/Invoke and
marshaling in the documenation. There is a sepecific newsgroup
for interop microsoft.public.dotnet.framework.interop where
you can ask questions regarding this.

Hope this helps,

//Andreas
 
Here is an example:


[DllImport("gdiplus.dll", ExactSpelling=true)]
internal static extern int GdipCreateBitmapFromGdiDib( IntPtr bminfo,
IntPtr pixdat, ref IntPtr image );


I didn't call dll's from C# myself, but above is an example of calling a
system func.
Code is taken from Twain Library iinterface ported to C#, which i use for
scanning capability in my app.
 
Please note that it is not always as trivial as this example. You will have
to examine how you will call the API, for example it may enable you to
pass NULL instead of a reference to a struct (and a struct may be passed
by using Out or Ref, depending how the API will use the struct) which
will change the signature of your interop call.

Another issue are strings. C/C++ have a whole army of different string
types and you need to be aware of this when you pass a string to an
API. You can also use the StringBuilder in some situations where you
expect a buffer to be filled by the API.

This is why I recommend that you read the documentation which is
shipped with .NET Framework and is avalible on MSDN if you don't
have it installed.

Hope this helps,

// Andreas
 
thanks all of above!!!
i find a example ,and take it show!!!

/////===============test.cs file=============================

using System;
using System.Runtime.InteropServices;//this is important!!!
public class InvokeDll {
[DllImport("MyLib.dll", CharSet=CharSet.Auto)]
static extern int add(int a,int b);//interface of out dll file
public static void Main() {
Console.WriteLine(add(10,30));
}
}

///==================================================

///=================mylib.c file=================================

__declspec(dllexport) int __cdecl add(int, int);

int add(int a,int b) {
return a+b;
}


///==================================================
 
I agree with you, but my experience tells me that sometimes you just don't
know where to start,
e.g. what's the syntax for calling a *.dll, for the beginning.
In such case common words like:
"I recommend you read about p/Invoke and marshaling in the documenation"
just don't give one a clue,
though I 100% agree with you that he needs to read mentioned docs.
But sometimes you just need to get a general idea and you will be able to go
further yourself from that,
that's why sometines it's more help if you just show a simple example to
begin with.

Andrey



Andreas Håkansson said:
Please note that it is not always as trivial as this example. You will have
to examine how you will call the API, for example it may enable you to
pass NULL instead of a reference to a struct (and a struct may be passed
by using Out or Ref, depending how the API will use the struct) which
will change the signature of your interop call.

Another issue are strings. C/C++ have a whole army of different string
types and you need to be aware of this when you pass a string to an
API. You can also use the StringBuilder in some situations where you
expect a buffer to be filled by the API.

This is why I recommend that you read the documentation which is
shipped with .NET Framework and is avalible on MSDN if you don't
have it installed.

Hope this helps,

// Andreas

MuZZy said:
Here is an example:


[DllImport("gdiplus.dll", ExactSpelling=true)]
internal static extern int GdipCreateBitmapFromGdiDib( IntPtr bminfo,
IntPtr pixdat, ref IntPtr image );


I didn't call dll's from C# myself, but above is an example of calling a
system func.
Code is taken from Twain Library iinterface ported to C#, which i use for
scanning capability in my app.
 
this is a very good intro (which goes on to talk about using a native dll
which was compiled via C++)
http://www.csharphelp.com/archives2/archive402.html

--
Br,
Mark Broadbent
mcdba,mcse+i
=======================
jebelsea said:
thanks all of above!!!
i find a example ,and take it show!!!

/////===============test.cs file=============================

using System;
using System.Runtime.InteropServices;//this is important!!!
public class InvokeDll {
[DllImport("MyLib.dll", CharSet=CharSet.Auto)]
static extern int add(int a,int b);//interface of out dll file
public static void Main() {
Console.WriteLine(add(10,30));
}
}

///==================================================

///=================mylib.c file=================================

__declspec(dllexport) int __cdecl add(int, int);

int add(int a,int b) {
return a+b;
}


///==================================================







MuZZy said:
Here is an example:


[DllImport("gdiplus.dll", ExactSpelling=true)]
internal static extern int GdipCreateBitmapFromGdiDib( IntPtr bminfo,
IntPtr pixdat, ref IntPtr image );


I didn't call dll's from C# myself, but above is an example of calling a
system func.
Code is taken from Twain Library iinterface ported to C#, which i use for
scanning capability in my app.
 
Back
Top