Msdn Moderate Dllimportattribute The explanation is as follows:This attribute can be applied to methods. Dllimportattribute Property provides DLL Information required for calling the exported function. As the minimum requirement, DLL . An example is provided: [Dllimport ("kernel32.dll", entrypoint = "movefilew", setlasterror = true, Charset = charset. Unicode, exactspelling = true, Callingconvention = callingconvention. stdcall)] Public static extern bool movefile (string SRC, string DST ); I searched the internet and used it to call it. Win32 Of API As shown above. Or call C Or C ++ Compiled DLL . This is never used. Just a few days ago, I suddenly assigned a temporary task and made a little thing about "parking factory management". I heard that a child from a large cadre wants to get such a thing. The cadre is a company customer, after the discussion, I asked you to make such a small thing. The single-chip microcomputer is used to simulate the incoming and outgoing signals of vehicles. I have no idea about single-chip microcomputer. Fortunately, someone has written a round-robin single-chip microcomputer. DLL , I only want to call, because it is C ++ Write, so DLL Copy Bin After the directory (This Dllimport FromProgramStart the Directory and start searching for DLL , Go System32 Search ), Use Dllimport But encountered several problems in this process:
1. Have a look C ++ OfCode, You only need to use three methods: Bool openserialport1 () Bool fngetio (unsigned char & P1, unsigned char & p2, unsigned char & P3) Bool closeserialport1 () So I wrote in my own program: Using System. runtime. interopservices; ...... [Dllimport("Getiodll. dll", Entrypoint ="Openserialport1")] Public Static Extern BoolOpenserialport1 (); [Dllimport("Getiodll. dll", Entrypoint ="Fngetio")] Public Static Extern BoolFngetio (Ref ByteP1,Ref ByteP2,Ref ByteP3 ); [Dllimport("Getiodll. dll", Entrypoint ="Closeserialport1")] Public Static Extern BoolCloseserialport1 (); However, the program always prompts"
No entry point found", I had to search the Internet, and finally called Exists Small software , View DLL, As shown in the post , DLL The function name in has changed , They are: ? Openserialport1 @ ya_nxz ? Fngetio @ ya_naae00 @ Z ? Closeserialport1 @ ya_nxz Place these strange names Entrypoin .
2. However, after the single-chip microcomputer is connected, the problem arises again. Although the result returned by openserialport1 is true, all data read by fngetio is 0, which should be exactly 1. I came to a colleague and said there was a source code. I tried to turn the original DLL into a standard C. I didn't understand the Standard C, and I asked the person to change it, copy the compiled DLL to the bin of your program, replace entrypoin with the normal function name, and run it. This is really OK. Read/write . Ini File Dllimport But now . Ini The file seems to be used less. The following is a program for reading and writing: { 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 (); } } About Dllimport Many of the problems are that the parameters of the called method are complicated, and I haven't used them yet. I see a post, with pointers in the parameters, which will be recorded first for future query: The parameter uses a pointer to obtain an array: Int getdata (byte * pbuffer) Pbuffer Is the first address of the array, that is Getdata Write Pbuffer [0], pbuffer [1]... pbuffer [100]; Answer: [Dllimport ("yourdllfile. dll"] Private Static extern int getvalue ([financialas (unmanagedtype. lparray)] Byte [] pvalue ); If yes Out Parameters, which can be as follows: [Dllimport ("yourdllfile. dll")] Private Static extern int getvalue ([out, financialas (unmanagedtype. lparray)] Byte [] pvalue );
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1438350