|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
There is a code in VB.NET. The code utilizes some vendor made API by declaring Lib functions.
A function will pass a parameter and the result will save in the parameter. Code:
Public Declare Function GMW_DB_Read Lib "Gm7s32.dll" (ByVal IArea As Integer, ByVal strField As String, ByVal strbuf As String, ByVal strBufSize As Integer) As Integer Here is how we use it: Code:
strGMW_DB_Read_Buf = Space(41)
intGMW_DB_Read_Result = GMW_DB_Read(intGMW_DBC1_Open, "CONTACT", strGMW_DB_Read_Buf, 41)
strGMW_DB_Read_Buf = strGMW_DB_Read_Buf.Trim(New Char() {" ", Chr(0)})
If I use Msgbox "[" + strGMW_DB_Read_Buf + "]", it prompts Code:
[the result Look, the last bucket misses. However, the above needed to be converted to C#. My conversion is like: Code:
[DllImport("Gm7s32.dll")]
public static extern int GMW_DB_Read(int lArea, String strField, String strBuf, int strBufSize);
Code:
String strGMW_DB_Read_Buf = "";
int intGMW_DB_Read_Result = GMW_DB_Read(intGMW_DBC1_Open, "ACCOUNTNO", strGMW_DB_Read_Buf, 41);
The value of strGMW_DB_Read_Buf is still empty! If I change the declaration Code:
[DllImport("Gm7s32.dll")]
public static extern int GMW_DB_Read(int lArea, String strField, ref String strBuf, int strBufSize);
There will be an unknown runtime error. What should I do? Please help.
__________________
------------------------------------------ Perl Kids Kiss Perl Stanley ------------------------------------------ |
|
#2
|
||||
|
||||
|
Quote:
in VB.NET you should use & to concat 2 strings, + is used in C# |
|
#3
|
||||
|
||||
|
Quote:
stanley1610: Set a BreakPoint in your VB.NET version and carefully go one step at a time, observing in the Locals window (or by hovering your mouse cursor over variables) the actual runtime values of the strings. In C#, you would be able to see "[the result\0]", because when displaying runtime string values, quotes are added to denote a string. However, in VB.NET, there is a different clue. For a normal string, quotes would be added to the beginning and end. So "[the result]" would show in the Locals window as "[the result]". If there is a null character just before the end bracket, VB.NET won't display it as \0 the way C# does, but it also won't show the rest of the string, including the end quote that it adds. It would appear as "[the result instead of "[the result". Every other string value would have two quotes in the Locals window, but one with a null character would have only the beginning quote. One possible reason your C# buffer is always empty is that in your VB.NET version, you set it to a string of 41 spaces, but the C# version is just an empty string. You didn't rewrite it in the same way. |
|
#4
|
||||
|
||||
|
Thanks a lot for your reply.
Yes, my guess is similar to your opinion. The result might be result\0 instead of result, therefore the output cannot show the characters following \0. However, my task is to make C# code work. I try to declare the string with the value of 41 spaces (i.e. " " ![]() It does not work either. I think it relates to pass by reference but it prompts runtime error. How can I rewrite it? Many thanks. Quote:
|
|
#5
|
||||
|
||||
|
I had an idea....
There is this program called Reflector that allows one to open an assembly and view the MSIL and structure and everything. It can also translate the MSIL into [mostly] equivalent VB.NET or C#. So I grabbed an old VB.NET project, added a module, and pasted this in it: Code:
Public Declare Function GMWDBRead Lib "Gm7s32.dll" (ByVal IArea As Integer, ByVal strField As String, ByVal strbuf As String, ByVal strBufSize As Integer) As Integer Code:
[DllImport("Gm7s32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern int GMWDBRead(int IArea, [MarshalAs(UnmanagedType.VBByRefStr)] ref string strField, [MarshalAs(UnmanagedType.VBByRefStr)] ref string strbuf, int strBufSize);
|
|
#6
|
||||
|
||||
|
|
|
#7
|
||||
|
||||
|
Many thanks for your reply. Your code can work.
However, when I prompt the variable: Console.WriteLine("strGMW_DB_Read_Buf:[" + strGMW_DB_Read_Buf.Trim() + "]"); The result string cannot be trimmed by Trim(). Is it due to UnmanagedType? How can I turn it back to normal string? Thanks for your help Quote:
|
|
#8
|
||||
|
||||
|
Have you tried assigning it to a new [normal] String variable?
|
|
#9
|
||||
|
||||
|
Quote:
strGMW_DB_Read_Buf.SubString(0, 20).Trim(); |
|
#10
|
||||
|
||||
|
Quote:
|
|
#11
|
||||
|
||||
|
I would guess not. If it's still just character buffer there's not substring method, and C# isn't one to do implicit conversions for you...
__________________
Primary Forums: .Net Development, MS-SQL, C Programming VB.Net: It's not your father's Visual Basic. [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers] |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > .Net Development > Convert VB.NET To C#.NET |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|