Summary about C # Calling VC SDK

Source: Internet
Author: User

Because the SDK must be developed based on the Development Kit. Therefore, you must call the SDK development interface of VC in C. And call the dynamic link library DLL file in C.

C # There are two methods to call the dynamic link library. One is to directly Reference COM and add Reference directly to the project. The other is DllImport. Here we use the latter.

Reference is actually very simple.

Add a space set

Using System. Runtime. InteropServices;

Define a class to convert the VC method into C #.

 

Class CanonCamera
{
[DllImport ("... \ Lib \ PRSDK. dll")]
Public static extern uint PR_StartSDK ();

[DllImport ("... \ Lib \ PRSDK. dll", CharSet = CharSet. Ansi)]
Public static extern uint PR_GetDllsVersion (
Ref uint pBufferSize,
Ref prType. prDllsVerInfo pDllVersion
);
}

Then you can call it directly. It is easy to use. However, the conversion is more difficult, because the pointer, array, and structure definitions in VC are not the same as those in C. In particular, the structure requires an unmanaged approach.

Typeof unsigned long prUInt32;

PrCAPI PR_GetDllsVersion (
PrUInt32 * pBufferSize,
PrDllsVerInfo * pDllVersion
);

For int * a in VC, the input parameter of this pointer can be passed in C # using the ref method. And ref int;

Structure. Pay special attention to the conversion

Define a structure in VC

Typedef struct {
PrWChar ModuleName [512];/* Module name (512 characters )*/
PrWChar Version [32];/* Version (32 characters )*/
} PrVerInfo;

Typedef struct {
PrUInt32 Entry;/* Number of modules encoded in this structure */
PrVerInfo VerInfo [1];/* Array of file version number information of PS-ReC SDK modules */
} PrDllsVerInfo;

So we need to define this in C #.

[StructLayout (LayoutKind. Sequential)]
Public struct prVerInfo {
[Financialas (UnmanagedType. ByValArray, SizeConst = 512)]
Public ushort [] ModuleName;/* Module name (512 characters )*/
[Financialas (UnmanagedType. ByValArray, SizeConst = 32)]
Public ushort [] Version; // 32 characters
}

[StructLayout (LayoutKind. Sequential)] // Sequential Layout
Public struct prDllsVerInfo {
Public uint Entry;
[Financialas (UnmanagedType. ByValArray, SizeConst = 1)] // send data between hosted code and unmanaged code
Public prVerInfo [] VerInfo; // prANY
};

Now you can call the SDK function.

 

PrDllsVerInfo prDllVerInfo = new prDllsVerInfo ();

Err = CanonCamera. PR_GetDllsVersion (ref buffersize, ref prDllVerInfo); // OK

The basic value types should be clear today's gains.

VC unsigned long c # uint ulong System. UInt32; // 32-bit

VC unsigned shot c # ushot System. UInt16; // 16-bit

I feel that C # has been learning for a long time, and I have forgotten it.

Another ASCII and String Conversion Function.

 

Public string Chr (int asciiCode)
{
If (asciiCode> = 0 & asciiCode <= 255)
{
System. Text. ASCIIEncoding asciiEncoding = new System. Text. ASCIIEncoding ();
Byte [] byteArray = new byte [] {(byte) asciiCode };
String strCharacter = asciiEncoding. GetString (byteArray );
Return (strCharacter );
}
Else
{
// Throw new Exception ("ASCII Code is not valid."); // Chinese Character
Return "abnormal ";
}
}

Public int Asc (string character)
{
If (character. Length = 1)
{
System. Text. ASCIIEncoding asciiEncoding = new System. Text. ASCIIEncoding ();
Int intAsciiCode = (int) asciiEncoding. GetBytes (character) [0];
Return (intAsciiCode );
}
Else
{
Throw new Exception ("Character is not valid .");
}

}

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.