Answered by:
Delphi String Marshaling in VB.NET

Question
-
Hello All,
I've got some functions I need to call that are in some DLL files that have been written in Delphi 5 in a VB.NET 2005 application. I'm able to use the DllImport function to access the functions just fine; however, I'm having an issue with the Delphi string data type and passing it to and from the .NET application.
Take this delphi function for example:
function AllocateInventory2(ItemNo: string; Warehouse: string; Quantity: double): Boolean; begin result := AllocateInv(UpperCase(ItemNo), '', UpperCase(Warehouse), Quantity); end;
It takes two string parameters and one double. I can call this function from VB.NET just fine, but it will error as soon as it tries to use one of the string variables (so in this case, when it passes them to another function) and I've found that the string values in Delphi are junk when they've been passed to .NET. I'm guessing that the string objects behave and/or are encoded differently between .NET & Delphi 5, and that I have to apply some sort of Marshaling to the function parameters in .NET, but I've tried all of the different types of marshaling and nothing has worked. Unfortunately, I haven't been able to find anything about this issue on the internet.
Does anyone know how I can call this Delphi function from .NET and have it pass the string values in a manner so that they won't be corrupted when stored in Delphi?
- Edited by WaluigiCubed Tuesday, January 24, 2012 5:52 PM
Tuesday, January 24, 2012 5:24 PM
Answers
-
Hi WaluigiCubed,
According to your description, it seems that this issue is the same with your another thread: http://social.msdn.microsoft.com/Forums/en-GB/vbgeneral/thread/2f0a7215-c843-410b-addd-66b95a979f5a
And you have found a workaround (rewrite your code )in that thread. So what happened now?
Anyway, my new suggestion is, use the StringBuilder to replace the string in VB.NET when there is a string in Delphi.
For example:
You can declare the method AllocateInventory2 in VB.net like this way:
private shared function AllocateInventory2(ItemNo as StringBuilder, Warehouse as Stringbuilder, quantity as double) As Boolean
End Function
I hope this will be helpful.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Proposed as answer by Cor Ligthert Thursday, January 26, 2012 11:08 AM
- Marked as answer by Mike Feng Thursday, February 9, 2012 9:36 AM
Thursday, January 26, 2012 9:11 AM
All replies
-
A string is another way of an special array of characters. Just a name which came because that name was to long.
So can you show an example of your what you call "Delphi" string.
I get more the idea it is an encoding problem.
http://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.100).aspx
Success
CorWednesday, January 25, 2012 8:31 AM -
Hi WaluigiCubed,
According to your description, it seems that this issue is the same with your another thread: http://social.msdn.microsoft.com/Forums/en-GB/vbgeneral/thread/2f0a7215-c843-410b-addd-66b95a979f5a
And you have found a workaround (rewrite your code )in that thread. So what happened now?
Anyway, my new suggestion is, use the StringBuilder to replace the string in VB.NET when there is a string in Delphi.
For example:
You can declare the method AllocateInventory2 in VB.net like this way:
private shared function AllocateInventory2(ItemNo as StringBuilder, Warehouse as Stringbuilder, quantity as double) As Boolean
End Function
I hope this will be helpful.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Proposed as answer by Cor Ligthert Thursday, January 26, 2012 11:08 AM
- Marked as answer by Mike Feng Thursday, February 9, 2012 9:36 AM
Thursday, January 26, 2012 9:11 AM