About DllImport in C #

Source: Internet
Author: User
Tags array bool ini int size key

The explanation for DllImportAttribute in MSDN is this: You can apply this attribute to a method. The DllImportAttribute property provides the information necessary to invoke a function that is exported from an unmanaged DLL. As a minimum requirement, you must provide the name of the DLL that contains the entry point.

And give an example:

[DllImport ("KERNEL32.") DLL ", entrypoint=" Movefilew ", Setlasterror=true,

CharSet=CharSet.Unicode, Exactspelling=true,

Callingconvention=callingconvention.stdcall)]

public static extern bool MoveFile (string src, string dst);

Search the Internet, the most common is to use it to invoke the WIN32 API, for example, as shown above. or call a DLL written in C or C + +.

This thing hasn't been used much. Just a few days ago suddenly assigned to a temporary task, to do a "parking plant management" of small things, I heard that a big cadre of children to get such a thing, that cadres are the company's customers, discuss serious matters and please us to do such a small thing. The use of a single chip computer simulation of vehicle access to some signals.

I don't have a single chip computer. Fortunately, someone wrote a poll of the DLL, I just call, because it is written in C + +, and then copy the DLL to the bin directory (this DllImport will start from the program startup directory to find the name of the DLL, not found then go to the system32 under lookup), Called with DllImport, but there are several problems encountered in this process:

1. After looking at the code of C + +, there are only three methods that need to be used:

BOOL OpenSerialPort1 ()

BOOL Fngetio (unsigned char& P1, unsigned char& P2, unsigned char& P3)

BOOL CloseSerialPort1 ()

So I wrote it in my own program:

Using System.Runtime.InteropServices;

......

[DllImport ("GetIODll.dll", EntryPoint = "OpenSerialPort1")]

public static extern bool OpenSerialPort1 ();

[DllImport ("GetIODll.dll", EntryPoint = "Fngetio")]

public static extern bool Fngetio (ref byte P1, ref byte P2, ref byte P3);

[DllImport ("GetIODll.dll", EntryPoint = "CloseSerialPort1")]

public static extern bool CloseSerialPort1 ();

However, the program at run time always prompts "No entry point found" make a Meng, had to search the Internet, and finally down a small software called Exescope, after the installation of the DLL, and sure enough to write in the post, the function name in the DLL has changed, respectively:

? openserialport1@ @YA_NXZ

? fngetio@ @YA_NAAE00 @z

? closeserialport1@ @YA_NXZ

Put these strange names into the Entrypoin, compile and run, no problem.

2. But after the MCU, the problem comes again, although the result of OpenSerialPort1 return is true, but Fngetio read out a data is all 0, should be all 1 is right. Came to a colleague, said anyway has the source code, the original DLL into standard C to try, standard C non-standard c I also did not understand, let that person to change a bit, after compiling the DLL to their own program bin, will entrypoin into the normal function name, run, this time is really OK.

When reading and writing to the. ini file, dllimport is also used, but now the. ini file seems to be used less, the following is a read-write program:

{

Publicstring path;

[DllImport ("kernel32")]

Privatestaticexternlong writeprivateprofilestring (String section,string key,string val,string filePath);

[DllImport ("kernel32")]

Privatestaticexternint getprivateprofilestring (String section,string key,string Def,stringbuilder

Retval,int size,string FilePath);

Public IniFile (String INIPath)

{

Path = INIPath;

}

Publicvoid iniwritevalue (String section,string key,string Value)

{

WritePrivateProfileString (Section,key,value,this.path);

}

Publicstring inireadvalue (String section,string Key)

{

StringBuilder temp = new StringBuilder (255);

int i = getprivateprofilestring (Section,key, "", Temp,255,this.path);

Return temp. ToString ();

}

}

A lot of problems about DllImport on the internet is because the parameter of the method is more complicated, now I have no use to, see a post, the parameters of the pointer, also recorded to prepare for future search:

parameter is a pointer to get an array: Int GetData (BYTE * pbuffer)

Pbuffer is the first address of the array, which means that GetData will write pbuffer[0],pbuffer[1]....pbuffer[100];

Answer Yue:

[DllImport ("YourDllFile.dll"]

Private static extern int GetValue ([MarshalAs (UnmanagedType.LPArray)]byte[] pvalue);

If it is an out parameter, you can do the following

[DllImport ("YourDllFile.dll")]

Private static extern int GetValue ([Out,marshalas (UnmanagedType.LPArray)]byte[] pvalue);



Related Article

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.