C # dynamic calling of unmanaged DLL)

Source: Internet
Author: User

I tried to call some DLL previously written by Delphi under C #, basically implementing dynamic calling, passing in callback functions, and calling functions with structured array pointers as parameters.

Although dllimport Can Call DLL Functions statically, a new method named Marshal. getdelegateforfunctionpointer is added in. net2.0 to convert unmanaged function pointers to delegates. With this method, you can use three windows API functions: loadlibrary, getprocaddress, and freelibrary to dynamically Call DLL. Below are static classes for dynamic DLL calls.

Using system;

Using system. Collections. Generic;

Using system. text;

Using system. runtime. interopservices;

Using system. Data;

 

Namespace Test

{

/// <Summary>

/// DLL loading class

/// </Summary>

Internal class dynamicloaddll

{

[Dllimport ("Kernel32")]

Public static extern int getprocaddress (INT handle, string funcname );

 

[Dllimport ("Kernel32")]

Public static extern int loadlibrary (string funcname );

 

[Dllimport ("Kernel32")]

Public static extern int freelibrary (INT handle );

 

 

Public static delegate getaddress (INT dllmodule, string functionname, type T)

{

Int ADDR = getprocaddress (dllmodule, functionname );

If (ADDR = 0)

Return NULL;

Else

Return marshal. getdelegateforfunctionpointer (New intptr (ADDR), t );

}

}

 

Public class referdll

{

Private int m_hdll = 0; // DLL handle
Private delegate int showform (intptr ahandle );
Private showform m_showform;
Private const string dllnaem = "XXX. dll ";

M_hdll = dllloader. loadlibrary (dllnaem );
If (m_hdll = 0)
{MessageBox. Show ("loading failed! ")
Return;
}
M_showform = (showform) dllloader. getaddress (m_hpacsview, "showform", typeof (showform ));
// Use showform
If (m_showform! = NULL)
M_showform (ihandle );
// Uninstall the DLL
Dllloader. freelibrary (m_hdll );

}

}

Next, let's talk about how to call a function with a structured array pointer in the DLL as a parameter. It is defined in Delphi as follows:

// A structure is defined as follows:
Tstudyrec = record
UID: array [0 .. 127] of char;
End;

Tchararray = array [0 .. 49] of tstudyrec;

// The dll contains the following functions. astudys is the tchararray pointer.
Function open (astudys: pointer): hresult; stdcall;

To call normally in C #, first define the same structure

Private struct studyrec
{
[Financialas (unmanagedtype. byvaltstr, sizeconst = 128)] Public String uid; // name
}

Then declare a delegate
Private delegate int open (intptr astudys );
Obtain delegated instance from DLL

Private Open m_open = (open) dllloader. getaddress (m_hdll, "open", typeof (open); // m_hdll is the DLL pointer

So far, the same results and functions have finally been defined in C #. The following is the call, because the structure array is intended for unmanaged DLL, therefore, the most important thing is to use marshal. allochglobal allocates the unmanaged memory, puts the structure array in the memory, and then calls the memory pointer as a parameter.

Studyrec [] arrstudyrec = new studyrec [50];
Int isize = marshal. sizeof (typeof (studyrec ));
Intptr parrstudyrec = marshal. allochglobal (marshal. sizeof (isize * 50 ));
Int run = (INT) parrstudyrec;

For (INT I = 0; I <50; I ++)
{

Arrstudyrec [I]. uid = I. tostring (); // This is just a simulation, So I is treated as uid directly.
Marshal. structuretoptr (arrstudyrec [I], (intptr) Run, false );
Run + = isize;

}

M_open (parrstudyrec );

 

From: http://zhouweigang01.blog.163.com/blog/static/934090720095493459311/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.