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);