This is three online collection technologiesArticleAnd explains how to create Win32 DLL, and then how to call this DLL in C.
First, create a Win32 DLL article. This article is everywhere. Here is an article I have read: http://www.flipcode.com/articles/article_creatingdlls.shtml. The creation of Win32 dll has provided a good template in Visual Studio, but you must note that some xxx_api macros did not add extern "C, in this case, the function cannot be found in C. Therefore, remember to define the function as extern "C ".
--------------------------------------------------------------------------------
The second article is from http://edu.100down.com/it/program/csharp/105256797.html. The following content is a reference:
Platform call service (pinvoke) allows hostingCodeCall the unmanaged functions implemented in the DLL.
This tutorial describes how to call an unmanaged DLL function from C. The attributes discussed in this tutorial allow you to call these functions and correct data types.
Tutorial
C # The Code has the following two methods to directly call the unmanaged code:
Directly call the function exported from the DLL.
Call the interface methods on the COM Object (for more information, see the first part of COM InterOP: C # client tutorial ).
Both of these technologies must provide the C # compiler with an unmanaged function declaration, you may also need to provide the C # compiler with instructions on how to mail parameters and return values passed between the unmanaged code.
This tutorial consists of the following topics:
Export data directly from C # by calling DLL
By default, mails are sent and custom mails are specified for Parameters of unmanaged methods.
Specify custom sending and receiving processing for user-defined structures
Registration callback Method
This tutorial includes the following examples:
Example 1 use dllimport
Example 2 rewrite the default mail sending Process
Example 3 specify custom sending and receiving
Export data directly from C # by calling DLL
To declare a method to implement DLL export, perform the following operations:
Use the C # keyword static and extern to declare the method.
Attaches the dllimport attribute to this method. The dllimport attribute allows you to specify the name of the DLL containing the method. Generally, the C # method is named with the same name as the exported method, but different names can also be used for the C # method.
You can also specify custom sending and receiving information for the parameters and returned values of the method, which will overwrite the default sending and receiving information of. NET Framework.
Example 1
This example shows how to use the dllimport attribute to output messages by calling puts in msvcrt. dll.
// Pinvoketest. CS
Using system;
Using system. runtime. interopservices;
Class platforminvoketest
{
[Dllimport ("msvcrt. dll")]
Public static extern int puts (string C );
[Dllimport ("msvcrt. dll")]
Internal static extern int _ flushall ();
Public static void main ()
{
Puts ("test ");
_ Flushall ();
}
} Output
Test code Discussion
The preceding example shows the minimum requirements for declaring the C # method implemented in an unmanaged DLL. The platforminvoketest. Puts method declares with the static and extern modifiers and has the dllimport attribute, which uses the default name puts to notify the compiler that this implementation comes from msvcrt. dll. To use different names (such as putstring) for the C # method, you must use the entrypoint option in the dllimport attribute, as shown below:
[Dllimport ("msvcrt. dll", entrypoint = "puts")] For more information about the syntax of the dllimport attribute, see the dllimportattribute class.
By default, mails are sent and custom mails are specified for Parameters of unmanaged methods.
When calling an unmanaged function from the C # code, the common language runtime must mail parameters and return values.
Each. NET Framework type has a default unmanaged type. The Common Language Runtime uses this unmanaged type to send data to hosted function calls. For example, the default sending type of C # string values is lptstr (pointer to the tchar character buffer. You can use the externalas attribute in the C # declaration of an unmanaged function to rewrite the default mail sending process.
Example 2
This example uses the dllimport attribute to output a string. It also shows how to rewrite the default mails of function parameters by using the externalas attribute.
// Marshal. CS
Using system;
Using system. runtime. interopservices;
Class platforminvoketest
{
[Dllimport ("msvcrt. dll")]
Public static extern int puts (
[Financialas (unmanagedtype. lpstr)]
String m );
[Dllimport ("msvcrt. dll")]
Internal static extern int _ flushall ();
Public static void main ()
{
Puts ("Hello world! ");
_ Flushall ();
}
} Output
When running this example, the string
Hello world! Will be displayed on the console.
Code Discussion
In the previous example, the default sending process for parameters of the puts function has been rewritten from the default value lptstr to lpstr.
The externalas attribute can be placed on the fields of the method parameter, method return value, and structure and class. To set the sending and receiving processing of the return value of a method, place the externalas attribute and the returned attribute position in the attribute block of the method. For example, to explicitly set the sending and sending processing of the return value of the puts method:
...
[Dllimport ("msvcrt. dll")]
[Return: financialas (unmanagedtype. I4)]
Public static extern int puts (
... For more information about the syntax of the externalas attribute, see the externalasattribute class.
Note that the in and out attributes can be used to annotate parameters of an unmanaged method. They work in a similar way as the in and out modifiers in the midl source file. Note that the out attribute is different from the C # parameter modifier out. For more information about the in and out attributes, see inattribute class and outattribute class.
Specify custom sending and receiving processing for user-defined structures
You can specify custom sending and receiving attributes for fields of structures and classes that are passed to an unmanaged function or returned from an unmanaged function. This can be done by adding the externalas attribute to the fields of the structure or class. You must also use the structlayout attribute to set the structure layout. You can also control the default encapsulation processing of string members and set the default encapsulation size.
Example 3
This example shows how to specify a custom sending and receiving processing attribute for the structure.
Consider the following C structure:
Typedef struct taglogfont
{
Long lfheight;
Long lfwidth;
Long lfescapement;
Long lforientation;
Long lfweight;
Byte lfitalic;
Byte lfunderline;
Byte lfstrikeout;
Byte lfcharset;
Byte lfoutprecision;
Byte lfclipprecision;
Byte lfquality;
Byte lfpitchandfamily;
Tchar lffacename [lf_facesize];
} Logfont; in C #, you can use structlayout and marshalas attributes to describe the preceding structure, as shown below:
// Logfont. CS
// Compile with:/Target: Module
Using system;
Using system. runtime. interopservices;
[Structlayout (layoutkind. Sequential)]
Public class logfont
{
Public const int lf_facesize = 32;
Public int lfheight;
Public int lfwidth;
Public int lfescapement;
Public int lforientation;
Public int lfweight;
Public byte lfitalic;
Public byte lfunderline;
Public byte lfstrikeout;
Public byte lfcharset;
Public byte lfoutprecision;
Public byte lfclipprecision;
Public byte lfquality;
Public byte lfpitchandfamily;
[Financialas (unmanagedtype. byvaltstr, sizeconst = lf_facesize)]
Public String lffacename;
} For more information about the structlayout attribute syntax, see structlayoutattribute class.
Then you can use this structure in the C # code, as shown below:
// Pinvoke. CS
// Compile with:/addmodule: logfont. netmodule
Using system;
Using system. runtime. interopservices;
Class platforminvoketest
{
[Dllimport ("gdi32.dll", charset = charset. Auto)]
Public static extern intptr createfontindirect (
[IN, financialas (unmanagedtype. lpstruct)]
Logfont lplf // Characteristics
);
[Dllimport ("gdi32.dll")]
Public static extern bool deleteobject (
Intptr handle
);
Public static void main ()
{
Logfont LF = new logfont ();
Lf. lfheight = 9;
Lf. lffacename = "Arial ";
Intptr handle = createfontindirect (LF );
If (intptr. Zero = handle)
{
Console. writeline ("can't creates a logical font .");
}
Else
{
If (intptr. size = 4)
Console. writeline ("{0: x}", handle. toint32 ());
Else
Console. writeline ("{0: x}", handle. toint64 ());
// Delete the logical font created.
If (! Deleteobject (handle ))
Console. writeline ("can't delete the logical font ");
}
}
} Running example
C30a0ae5 code Discussion
In the previous example, the createfontindirect method uses a logfont-type parameter. The Alas and in attributes are used to limit this parameter.ProgramThe value returned by this method is displayed as a hexadecimal capital string.
Registration callback Method
To register a managed callback that calls an unmanaged function, use the same parameter list to declare an instance of a delegate and pass it through pinvoke. On an unmanaged end, it is displayed as a function pointer. For more information about pinvoke and callback, see platform call details.
For example, consider the following unmanaged function myfunction, which requires callback as one of its parameters:
Typedef void (_ stdcall * pfn_mycallback )();
Int _ stdcall myfunction (PFN _ mycallback callback); To call myfunction from managed code, declare this delegate and append dllimport to the function declaration, and send any parameters or return values as needed:
Public Delegate void mycallback ();
[Dllimport ("mydll. dll")]
Public static extern void myfunction (mycallback callback); at the same time, ensure that the lifetime of the delegated instance overwrites the lifetime of the unmanaged code; otherwise, the Delegate will no longer be available after garbage collection.
--------------------------------------------------------------------------------
The third article is about NLP.
Description of unmanaged C-language managed classes in wtypes. h
Handle void * system. intptr 32-bit
Byte unsigned char system. byte 8-bit
Short short system. int16-bit
Word unsigned short system. uint16 16-bit
Int INT system. int32 32-bit
Uint unsigned INT system. uint32 32-bit
Long long system. int32 32-bit
Bool long system. int32 32-bit
DWORD unsigned long system. uint32 32-bit
Ulong unsigned long system. uint32 32-bit
Char char system. Char is modified with ANSI.
Lpstr char * system. string or system. stringbuilder is modified with ANSI.
Lpcstr const char * system. string or system. stringbuilder is modified with ANSI.
Lpwstr wchar_t * system. string or system. stringbuilder is modified with Unicode.
Lpcwstr const wchar_t * system. string or system. stringbuilder are modified with Unicode.
Float float system. Single 32-bit
Double double system. Double 64-bit
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/tangl_99/archive/2006/09/06/1182354.aspx