C # Call an unmanaged DLL parameter transfer Memorandum

Source: Internet
Author: User

When the unmanaged parameters are const mystruct * mystructinstance and mystruct * mystructinstance, the C # program can pass the parameters through ref mystruct mystructinstance.
In 2, c ++, and C, the long value is 4 bytes, and the conversion to C # Is int type.
For example
Definition in C, C ++
Typedef struct hs_rect
{
Long left;
Long top;
Long right;
Long bottom;
} Hs_rect;
In C #, it should be
[Structlayout (layoutkind. Sequential, charset = charset. ANSI)]
Public struct hs_rect
{
Public int left;
Public int top;
Public int right;
Public int bottom;
};
3. Convert struct to pointer type
// Define an eyepoint as an hs_eyepoint struct.
Featuredata. hs_eyepoint eyepoint;
// Define peye as a pointer to hs_eyepoint
Intptr peye = marshal. alloccotaskmem (marshal. sizeof (typeof (featuredata. hs_eyepoint )));
// Convert eyepoint to peye, that is, let peye point to the eyepoint address
Marshal. structuretoptr (eyepoint, peye, true );
// Program Processing
Nretcode = hs_locateeye (pbygrayimage, nimagewidth [0], nimageheight [0],
Ref rcface, ref eyepoint );
// After processing, the pointer is restored to a struct.
Eyepoint = (featuredata. hs_eyepoint) Marshal. ptrtostructure (peye, typeof (featuredata. hs_eyepoint ));
// Release the space pointed to by the pointer
Marshal. freecotaskmem (pface );

Code:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. runtime. interopservices;

Namespace featureextraction
{
Public partial class form1: Form
{

// Call functions in C DLL
[Dllimport ("hisign_faceid.dll", entrypoint = "hs_initfaceidsdk", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
Public static extern int hs_initfaceidsdk (featuredata. hs_facemode enmode );

[Dllimport ("hisign_faceid.dll", entrypoint = "hs_getfacefeaturesize", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
Public static extern int hs_getfacefeaturesize ();

[Dllimport ("hisign_faceid.dll", entrypoint = "hs_getimagesize", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
Public static extern int hs_getimagesize (string strimagepathname, int [] nimagewidth, int [] nimageheight );
// Int _ stdcall hs_getimagesize (const char * strimagepathname, int * nimagewidth, int * nimageheight );

[Dllimport ("hisign_faceid.dll", entrypoint = "hs_getgrayimagefromfile", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
Public static extern int hs_getgrayimagefromfile (string strimagepathname, byte [] pbyoutgrayimage );
// Int _ stdcall hs_getgrayimagefromfile (const char * strimagepathname, byte * pbyoutgrayimage );

[Dllimport ("hisign_faceid.dll", entrypoint = "hs_facedetection", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
Public static extern int hs_facedetection (byte [] pbygrayimage, int nimagewidth, int nimageheight, int nminfacesize,
Int nmaxfacesize, ref featuredata. hs_facecandidate poutfacecand, int [] nfacenum );
// Int _ stdcall hs_facedetection (const byte * pbygrayimage, int nimagewidth, int nimageheight, int nminfacesize,
// Int nmaxfacesize, hs_facecandidate * poutfacecand, int * nfacenum );

[Dllimport ("hisign_faceid.dll", entrypoint = "hs_locateeye", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
Public static extern int hs_locateeye (byte [] pbygrayimage, int nimagewidth, int nimageheight, ref featuredata. hs_rect pfacerect, ref featuredata. hs_eyepoint peyepoint );
// Int _ stdcall hs_locateeye (const byte * pbygrayimage, int nimagewidth, int nimageheight,
// Const hs_rect * pfacerect, hs_eyepoint * peyepoint );

[Dllimport ("hisign_faceid.dll", entrypoint = "hs_faceextractionbyeyepos", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
Public static extern int hs_faceextractionbyeyepos (byte [] pbygrayimage, int nimagewidth, int nimageheight, ref featuredata. hs_eyepoint peyepoint, byte [] pbyfeature );
// Int _ stdcall hs_faceextractionbyeyepos (const byte * pbygrayimage, int nimagewidth, int nimageheight,
// Const hs_eyepoint * peyepoint, byte * pbyfeature );

[Dllimport ("hisign_faceid.dll", entrypoint = "hs_uninitfaceidsdk", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
Public static extern int hs_uninitfaceidsdk ();
// Int _ stdcall hs_uninitfaceidsdk ();

Public form1 ()
{
Initializecomponent ();

 

}

Private void button2_click (Object sender, eventargs E)
{
Int retcode = getdone ();
}

Private int getdone ()
{
Featuredata. hs_facemode enmode; // working mode of Face Recognition
Int nretcode; // return code
Featuredata. hs_facecandidate [] pfacecand; // a candidate for Face Detection
Int [] nfacenum = new int [1]; // number of detected faces
String strimagepath = @ "C: \ Documents ents and Settings \ kingking \ My Documents ents \ my pictures \ me \ 344.jpg"; // face image name
Int [] nimagewidth = new int [1]; // image width and height
Int [] nimageheight = new int [1];
Byte [] pbygrayimage = NULL; // grayscale image buffer
Featuredata. hs_eyepoint eyepoint = new featuredata. hs_eyepoint (); // eye position
Int nfeatsize; // obtain the number of bytes of facial features of the current SDK.
Byte [] pbyfeatures = NULL; // feature Buffer

// Initialize the SDK
Enmode = featuredata. hs_facemode.hs_verify;
Nretcode = hs_initfaceidsdk (enmode );
If (nretcode! = Featuredata. hisign_err_none)
{
Return nretcode;
}

// Obtain the size of the face features in bytes
Nfeatsize = hs_getfacefeaturesize ();

// Read the face image from the file and convert the face image into a grayscale image
Nretcode = hs_getimagesize (strimagepath, nimagewidth, nimageheight );
Pbygrayimage = new byte [nimagewidth [0] * nimageheight [0];
Nretcode = hs_getgrayimagefromfile (strimagepath, pbygrayimage );

Pfacecand = new featuredata. hs_facecandidate [100]; // allocate memory for Face Detection results

// Perform Face Detection
Nretcode = hs_facedetection (pbygrayimage, nimagewidth [0], nimageheight [0], 20,
(Nimagewidth [0]> = nimageheight [0]? Nimageheight [0]: nimagewidth [0]), ref pfacecand [0], nfacenum );

// Locate the eyes of each detected face
For (INT I = 0; I <nfacenum [0]; ++ I)
{
Featuredata. hs_rect rcface;
Rcface. Left = pfacecand [I]. trueleft;
Rcface. Top = pfacecand [I]. truetop;
Rcface. Right = pfacecand [I]. trueright;
Rcface. Bottom = pfacecand [I]. truebottom;

// Intptr prcface = marshal. alloccotaskmem (marshal. sizeof (rcface ));
// Marshal. structuretoptr (rcface, prcface, false );

Intptr pface = marshal. alloccotaskmem (marshal. sizeof (typeof (featuredata. hs_rect )));
Marshal. structuretoptr (rcface, pface, true );
Intptr peye = marshal. alloccotaskmem (marshal. sizeof (typeof (featuredata. hs_eyepoint )));
Nretcode = hs_locateeye (pbygrayimage, nimagewidth [0], nimageheight [0],
Ref rcface, ref eyepoint );

// Eyepoint = (featuredata. hs_eyepoint) Marshal. ptrtostructure (peye, typeof (featuredata. hs_eyepoint ));
Marshal. freecotaskmem (pface );
Marshal. freecotaskmem (peye );

// Extract features
Pbyfeatures = new byte [nfeatsize];
Nretcode = hs_faceextractionbyeyepos (pbygrayimage, nimagewidth [0],
Nimageheight [0], ref eyepoint, pbyfeatures );

Pbyfeatures = NULL;
// Marshal. freehglobal (prcface );
}

Pbygrayimage = NULL;
Pfacecand = NULL;

// Release SDK Resources
Nretcode = hs_uninitfaceidsdk ();

Return nretcode;

}
}


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.