In this article, we will focus on the columns. We can clearly see how to use C # To call C ++.Code.
Column: a standard Win32 API library cclic in C ++. in DLL, there is a function void * winapi getfunctionaddress (unsigned int Sn). This function obtains the function pointer, that is, the address of a function by passing the SN number. then call other functions through the returned address.
Then we must know the function structure corresponding to a Sn, such as Sn = 1-> bool winapi ccaskserverlicenseinfo (const char * server_address, unsigned short port, pcclic_info plicenseinfo)
In
Typedef struct _ cclic_info {
Char ower [64];
Unsigned short Manage_ip;
Unsigned short Ramained_ip;
Unsigned short Useable_time;
Unsigned char Type;
} Cclic_info, * pcclic_info;
The purpose of this column is to call the ccaskserverlicenseinfo function through C.
[Dllimport ( @" Cclic. dll " )]
Public Static Extern System. intptr matrix (system. uint32 Sn); // famous entry function
// Define the function pointer Model
Public Delegate System. int32 ccaskserverlicenseinfohandle (system. String servername, system. uint16 port, system. intptr );
Public static licenseinfo getlicentinfo (string Server, System. uint16 port)
{
System. intptr fptr = matrix (1); // obtainCcaskserverlicenseinfo addressCcaskserverlicenseinfohandle ccaskserverlicenseinfo=Marshal. getdelegateforfunctionpointer (fptr,Typeof(Ccaskserverlicenseinfohandle ))AsCcaskserverlicenseinfohandle; // converts an address to a function pointer in C #.
Licenseinfo info = New Licenseinfo (); // name structure and initialization
Intptr infoptr = Marshal. alloccotaskmem (marshal. sizeof (Info); // converts struct to pointer
Ccaskserverlicenseinfo (server, port, infoptr); // call a function
Info = (Licenseinfo) Marshal. ptrtostructure (infoptr, Typeof (Licenseinfo); // converts a pointer to a struct.
Return Info;
}
[Structlayout (layoutkind. Sequential, charset = Charset. ANSI)]
Public Struct Licenseinfo
{
[Financialas (unmanagedtype. byvalarray, sizeconst = 64 )]
Public System. Char [] ower;
Public System. uint16 manage_ip;
Public System. uint16 ramained_ip;
Public System. uint16 useable_time;
Public System. byte type;
}
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
-->
[dllimport ( @" cclic. DLL " )]
Public static extern system. intptr matrix (system. uint32 Sn);
Public DelegateSystem. int32 ccaskserverlicenseinfohandle (system. String servername, system. uint16 port, system. intptr );
Public Static Licenseinfo getlicentinfo (string Server, System. uint16 port)
{
System. intptr fptr = Matrix ( 8 );
Ccaskserverlicenseinfohandle ccaskserverlicenseinfo = Marshal. getdelegateforfunctionpointer (fptr, Typeof (Ccaskserverlicenseinfohandle )) As Ccaskserverlicenseinfohandle;
Licenseinfo info = New Licenseinfo ();
Intptr infoptr = Marshal. alloccotaskmem (marshal. sizeof (Info ));
Ccaskserverlicenseinfo (server, port, infoptr );
Info = (Licenseinfo) Marshal. ptrtostructure (infoptr, Typeof (Licenseinfo ));
Return Info;