When C # calls C + +, the string returns a parameter with an Access exception error

Source: Internet
Author: User

C + + function prototypes:

BOOL _stdcall Psstartup (HANDLE Hprintps, LPCSTR Lpszdatabasepath, LPCSTR lpszspoolfile);

Lpszspoolfile is the return parameter, which is assigned by the Psstartup and brought back.

The first statement I made in C # was:

[DllImport ("PrintScu.dll", callingconvention = callingconvention.stdcall)]public extern static int Psstartup (INTPTR Hprintps, string lpszdatabasepath, ref string lpszspoolfile);

However, an error occurred during the actual call:

System.accessviolationexception is unhandled  message=attempted to read or write protected memory. This is often a indication that other memory is corrupt.  Source=mscorlib  StackTrace: at       System.String.. ctor (sbyte* value) at       System.StubHelpers.CSTRMarshaler.ConvertToManaged (IntPtr CStr)

After finding a circle on the internet, I found that this declaration is not possible, you cannot use ref string, and you need to use StringBuilder, and then change to the following declaration, test pass:

[DllImport ("PrintScu.dll", callingconvention = callingconvention.stdcall)]public extern static int Psstartup (INTPTR Hprintps, String lpszdatabasepath, StringBuilder lpszspoolfile);

StackOverflow Post Address

When C # calls C + +, the string returns a parameter with an Access exception error

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.