Reputation: 564
So I have two string that were padded, but now I want them show and Trim() doesn't seem to want to do it.
String devicename = "".PadRight(100);
String deviceversion = "".PadRight(100);
bool isDeviceReady = capGetDriverDescriptionA(i, ref devicename, 100, ref deviceversion, 100);
later I use the strings like in the below:
Messagebox.show("Device Name="+devicename.Trim()+" , Device Version="+deviceversion.Trim());
All that is shown is "Device Name=name of the device
"
Thoughts?
Upvotes: 1
Views: 166
Reputation: 564
In the end it was pretty simple. Tomas Petrick was on the right track. It, the string, returned was a null terminated (also called a zero-terminated) string. Then I just needed to remove the null terminator from the strings.
devicename = devicename.trim('\0');
Upvotes: 0
Reputation: 243041
You could try using StringBuilder
instead of string
when calling an API function that returns a zero-terminated string. The behaviour you're getting probably follows from the fact that the API writes zero value at the end of the string (to terminate it), while .NET treats it as a normal string containing zero value.
Alternatively, look at this discussion, which suggests to annotate the parameter with [MarshalAs(UnmanagedType.LPWSTR)]
attribute. Then the PInovke mechanism should take care of zero value at the end automatically.
Upvotes: 1
Reputation: 24606
Why are you padding the strings at all? It looks like you are just filling the strings with spaces that will get replaced later by content, but judging from your question I don't see why this is necessary. What is wrong with just:
String devicename = "";
String deviceversion = "";
bool isDeviceReady = capGetDriverDescriptionA(i, ref devicename, 100, ref deviceversion, 100);
Upvotes: 2