How does delphi DLL output string (2) to C)

Source: Internet
Author: User

The DLL developed by Delphi Xe has two functions: test_returnpchar and test_returnpansichar:

Procedure movestr2pchar (const astr: widestring; const apchar: pwidechar; var apcharlen: integer); var oldlen: integer; slen: integer; begin oldlen: = apcharlen; slen: = length (astr); If apcharlen <slen + 1 then begin // requires the length plus the last #0 apcharlen: = slen + 1; Raise exception. createfmt (s_paramsizenotenough, ['apchar ', inttostr (oldlen), inttostr (slen + 1)]); end; // The number of returned characters, including the last #0 apcharlen: = slen + 1; move (astr [1], apchar ^, slen * 2); // write the final #0 pwidechar (INTEGER (apchar) + slen * 2) ^: = #0; end; Procedure moveansistr2pansichar (const astr: ansistring; const apchar: pansichar; var apcharlen: integer); var oldlen: integer; slen: integer; begin oldlen: = apcharlen; slen: = length (astr); If apcharlen <slen + 1 then begin // requires the length plus the last #0 apcharlen: = slen + 1; Raise exception. createfmt (s_paramsizenotenough, ['apchar ', inttostr (oldlen), inttostr (slen + 1)]); end; // The number of returned characters, including the last #0 apcharlen: = slen + 1; move (astr [1], apchar ^, slen); // write the final #0 pansichar (INTEGER (apchar) + slen) ^: = #0; end; function test_returnpchar (const apchar: pwidechar; var apcharlen: integer): hresult; var WS: widestring; begin result: = 0; try WS: = 'Software Co ., ltd. '; movestr2pchar (WS, apchar, apcharlen); expect t on E: exception do begin result: = 1000; g_lasterrormsg: = E. message; end; function test_returnpansichar (const apchar: pansichar; var apcharlen: integer): hresult; var S: ansistring; begin result: = 0; try s: = 'Software Co ., ltd. '; moveansistr2pansichar (S, apchar, apcharlen); expect t on E: exception do begin result: = 1000; g_lasterrormsg: = E. message; end;

That is, pass in a pwidechar/pansichar, and the function will modify its string.

In C #, because string is Unicode, pwidechar can be processed directly using string or stringbuilder. However, pansichar can only be processed using stringbuilder:

[DllImport("Stub.dll", EntryPoint = "Test_ReturnPChar", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int Test_ReturnPChar([MarshalAs(UnmanagedType.LPWStr)]string APChar,     ref int APCharLen);[DllImport("Stub.dll", EntryPoint = "Test_ReturnPAnsiChar", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int Test_ReturnPAnsiChar([MarshalAs(UnmanagedType.LPStr)]StringBuilder APChar,    ref int APCharLen);private void button1_Click(object sender, EventArgs e){        int len=1000;    string s=new string((char)0,len);    yjDllImport_TMTS.Test_ReturnPChar(s, ref len);    MessageBox.Show(s);    len = 1000;    //string s2 = new string((char)0, l);    StringBuilder sb = new StringBuilder(len);    yjDllImport_TMTS.Test_ReturnPAnsiChar(sb, ref len);    MessageBox.Show(sb.ToString());}
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.