Reputation: 18171
I have exported function in C++ DLL:
extern "C" int _stdcall info(int aError, char * description, int *aLen)
Trying to call it from C# application.
Declaring:
[DllImport("mydll.dll", CallingConvention = CallingConvention.StdCall)] public static extern int info(int aError, StringBuilder description, ref int length);
Calling:
StringBuilder buffer = new StringBuilder();
fr = info(5, buffer, ref 256);
From first point of view seems this works. But is it really safe? I mean will StringBuilder
always manage enaugh memory for string that comes from info
to buffer
variable?
I write StringBuilder description
in declaration, should I add ref
?
Upvotes: 0
Views: 487
Reputation: 141598
I mean will StringBuilder always manage enough memory for string that comes from info to buffer variable?
You should always specify the initial capacity of the StringBuilder by using the constructor that takes the capacity:
int size = 256;
StringBuilder buffer = new StringBuilder(256);
fr = info(5, buffer, ref size);
Upvotes: 2