How to call a function that contains IntPtr parameters in the com component?

Source: Internet
Author: User

How to call a function that contains IntPtr parameters in the com component?

Background

The company's payment platform recently docked with the payment interface of Xi'an mobile. The signature method in the interface is that the other party provides a com component, the component contains a signature method and a signature verification method. After the signature is registered, it is referenced in vs. After being referenced, view the definition of the component as follows:

using System;using System.Runtime.InteropServices;namespace UMPAYLib{    [ClassInterface(0)]    [Guid("E92EB0AA-00CC-4F93-A76D-632BEA94E980")]    [TypeLibType(2)]    [ComConversionLoss]    public class SignClass : ISign, Sign    {        public SignClass();        [DispId(1)]        public virtual string Sign(string str, string certfile, string keyfile);        [DispId(2)]        public virtual int Verify(string str, string sig, IntPtr certfile);    }}

 

First, let's take a look at the signature method: Sign (string str, string certfile, string keyfile );

The three parameters are the signature string, the path of the public key certificate, and the path of the Private Key Certificate.

Verify the signature again: Verify (string str, string sig, IntPtr certfile );

The three parameters are the signature string, the signature value to be verified, and the path of the Public Key Certificate.

So the question is, how is the certificate path of the third parameter of the Signing Method IntPtr type? What type is IntPtr? How can I call this method?

Solution Process

First of all, I asked the contact person of the interface provider, but the contact person of the interface provider was very responsible. He helped me read the document and asked my colleagues, but after reading their own documents, the contact person was also depressed, they do not know why the document is different from the method definition in the component. They say they want to ask for instructions from the headquarters, but the request from the headquarters should be sent via email, and there will be no reply for half a day. The interface will be launched for testing the day after tomorrow, when the reply seems unreliable, you have to rely on yourself, so you began to ask for help.

First, let's see what kind of IntPtr is?

MSDN explanation:

Platform-specific type used to indicate pointers or handles.
Note: The IntPtr type is designed as an integer and its size is applicable to a specific platform. That is to say, this type of instance will be 32-bit in 32-bit hardware and operating system, and 64-bit in 64-bit hardware and operating system. The IntPtr type can be used by languages that support pointers and can be used to reference data between languages that support or do not support pointers. IntPtr objects can also be used to keep the handle. For example, IntPtr instances are widely used in the System. IO. FileStream class to maintain file handles. The IntPtr type conforms to CLS, but the UIntPtr type does not. Only the IntPtr type is available in the runtime of the common language. Most UIntPtr types are provided to maintain the architectural symmetry between them and IntPtr types. This type implements the ISerializable interface.

One line is like this:

The IntPtr type can be used by languages that support pointers and can be used to reference data between languages that support or do not support pointers.

How can I reference data? Pointer? After seeing this, I entered the word "IntPtr character string" in Du Niang. The search result showed a blog written by a benevolent brother in the garden.

I first used the following method in this blog for testing:

/// <Summary> /// apply for an unmanaged Space Based on the Data Length // </summary> /// <param name = "strData"> apply for an unmanaged Space </param> /// <returns> pointer to the non-drag-pipe space </returns> private static IntPtr mallocIntptr (string strData) {// convert the string to Byte [] btData = System. text. encoding. default. getBytes (strData); // apply for a non-drag pipe space IntPtr m_ptr = Marshal. allocHGlobal (btData. length); // clear 0 Byte [] btZero = new Byte [btData. length + 1]; // be sure to add 1; otherwise, there will be garbled characters. The cause is not found. copy (btZero, 0, m_ptr, btZero. length); // assign Marshal to the space pointed to by the pointer. copy (btData, 0, m_ptr, btData. length); return m_ptr ;}

The test code is as follows:

UMPAYLib.SignClass signClass = new UMPAYLib.SignClass();
IntPtr ptrCertFile = mallocIntptr(certFile);int b = signClass.Verify(prestr, SIGN, ptrCertFile);

The page for running the test. When it is executed to signClass. when Verify is used to Verify the com component signature, it reports an exception "does not have enough memory to continue running the program", and the code inside the component cannot be seen, so I don't know how to deal with the exception that causes this memory overflow. I finally found the idea, but this problem occurs. What should I do? So I looked at the ptrCertFile attributes and methods and output the ptrCertFile. toString (), found a large number, but still do not know why memory overflow, I looked at it carefully, mallocIntptr this method, in this method, a memory allocation class has aroused my interest. It is Marshal. Let's take a look at what the Mashal class is doing? How can this problem be solved?

MSDN explanation:

Provides a method set for allocating unmanaged memory, copying unmanaged memory blocks, and converting hosted types into unmanaged types, other miscellaneous methods are also provided for interacting with non-hosted code.

It can be seen from the explanation that this class is mainly used to allocate unmanaged memory and convert between hosted and unmanaged types.

So I browsed the members of The Mashal class and found a method:

This method can also implement the above mallocIntptr function, so I changed the code to the following:

UMPAYLib.SignClass signClass = new SignClass();string certPath = MobileWapPayConfig.CertFile;IntPtr ptrCertFile = Marshal.StringToBSTR(certPath);int result = signClass.Verify(prestr, sign, ptrCertFile);Marshal.FreeBSTR(ptrCertFile);return result == 0;

Run the test page again. Everything is normal.

At this point, the problem of unknowing how to call IntPtr has been solved, but there is a small problem left over, that is why the mallocIntptr method will cause memory overflow. I hope you will know more about it!

Summary

For method parameters of some com components of the IntPtr type, you can use the corresponding methods of the Marshal class.

Reference page:

Http://www.yuanjiaocheng.net/CSharp/csharp-array.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-multi-dimensional-array.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-jagged-array.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-collection.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-arraylist.html

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.