[Original] C # function pointer for cross-platform Calling C ++

Source: Internet
Author: User

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;

 

 

 

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.