1

i have a function with 2 parameters in c. both of them are char *. one of them is input and another one is output and i don' know what should I fill the second one (output) please help me

 // C
 void func1(char * i_szInput , char * o_szOutput)
 {
    printf("%s\n" ,  i_szInput );
    strcpy(o_szOutput , "Hello");
 }

 // C#
 [DllImport("Test.dll")]
 public static void func1([MarshalAs(UnmanagedType.LPStr)] string str1, 
                          [MarshalAs(UnmanagedType.LPStr)] string str2);


// .......

 string str = null;
 func1("mytest" , str);
1
  • You should use StringBuilder Commented Aug 1, 2012 at 7:06

2 Answers 2

1

Use StringBuilder. From MSDN Marshaling Strings

String by reference: Passes strings as In/Out parameters using StringBuilder.

 [DllImport("Test.dll")]
 public static void func1([MarshalAs(UnmanagedType.LPStr)] String str1,
                          [MarshalAs(UnmanagedType.LPStr)] StringBuilder str2);

StringBuilder str = new StringBuilder();
func1("mytest", str);

See Default Marshaling for Strings: Fixed-Length String Buffers (MSDN).

Sign up to request clarification or add additional context in comments.

2 Comments

@Ria [MarshalAs(UnmanagedType.LPStr), Out()] StringBuilder str2 or [MarshalAs(UnmanagedType.LPStr)] out StringBuilder str2? And shouldn't you define a size for the StringBuilder?
Buffers Sample MSDN for marshalling unmanaged LPSTR type.
0

Try this:

string str = String.Empty;
func1("mytest", out str);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.