C # differences between Calling C and C ++ Functions

Source: Internet
Author: User

I recently made a secondary development of U800 phones. I needed to call the C function library of the manufacturer to call the phone. Later I wanted to add the call recording function. However, I found that the WAV file produced by the program is too large by default, I found a C ++ function library program for converting WAV to MP3, and a problem occurred. The following is a program interface (header file) for converting MP3 ):

 _MP3ENC_H_ mp3_enc( * inWavName, nRate, *

Declare a corresponding C # function interface according to the C # Call convention of the unmanaged program:

 [DllImport(, CharSet =    mp3_enc( inWavName,  nRate,  outMP3Name);

Then, call the function change function as follows:

 mp3_enc(txtWavFile.Text, , );

The following error message is displayed:

For the PInvoke function "U800Test! U800Test. Form1: mp3_enc "calls lead to stack asymmetry. The reason may be that the hosted PInvoke signature does not match the non-hosted target signature. Check whether the call conventions and parameters of the PInvoke signature match the non-hosted target signature.

Compared with the method used to call the C function library, it is found that there is no difference, but the C function code can be used normally:
Call C function interface:

USBDLL_API  _stdcall StartDial( iDevIdx, * szDest);    

Corresponding C # function interface:

  [DllImport(, CharSet =    StartDial( iDevIdx, szDest);


Let's take a look at the error message of the C ++ function call. Is the C ++ string type inconsistent? Must C ++ use Unicode characters? Change the function interface of C #:

 [DllImport(, CharSet =    mp3_enc( inWavName,  nRate,  outMP3Name);

Result C # The program crashes directly, and Try... Catch... is useless.

Finally, I searched the internet for half a day and found this statement:

DllImport also has a CallingConvention attribute. The default value is CallingCovention. Stdcall, which can be changed to Cdecl (c/c ++ default call method. This attribute must be specified in VS2010 to run. This problem does not exist in the same code in VS2008. It is strange...

The correct C ++ Function C # Call the interface should be like this:

[DllImport(, CharSet = CharSet.Ansi, CallingConvention =    mp3_enc( inWavName,  nRate,  outMP3Name);

 

 

 

 

 

 

 

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.