.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 28th, 2008, 11:09 PM
stanley1610's Avatar
stanley1610 stanley1610 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 250 stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 16 h 15 m 52 sec
Reputation Power: 8
Question Convert VB.NET To C#.NET

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
------------------------------------------

Reply With Quote
  #2  
Old April 29th, 2008, 08:15 AM
danielk's Avatar
danielk danielk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: Earth
Posts: 207 danielk User rank is Lance Corporal (50 - 100 Reputation Level)danielk User rank is Lance Corporal (50 - 100 Reputation Level)danielk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 23 h 20 m 15 sec
Reputation Power: 4
Quote:
Look, the last bucket misses

in VB.NET you should use & to concat 2 strings, + is used in C#

Reply With Quote
  #3  
Old April 29th, 2008, 09:37 AM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,610 LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 18 h 39 m 36 sec
Reputation Power: 379
Quote:
Originally Posted by danielk
in VB.NET you should use & to concat 2 strings, + is used in C#
If it were the operator, then the first square bracket concatenation wouldn't have worked. I think the buffer contains a null-terminated string, so it gets to the null and stops and doesn't display the closing bracket.

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.
__________________
Joel B Fant - LyonHaert.net

2 + 2 is... 10... in base 4

Reply With Quote
  #4  
Old April 29th, 2008, 08:11 PM
stanley1610's Avatar
stanley1610 stanley1610 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 250 stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 16 h 15 m 52 sec
Reputation Power: 8
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:
Originally Posted by LyonHaert
If it were the operator, then the first square bracket concatenation wouldn't have worked. I think the buffer contains a null-terminated string, so it gets to the null and stops and doesn't display the closing bracket.

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.

Reply With Quote
  #5  
Old April 30th, 2008, 10:32 AM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,610 LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 18 h 39 m 36 sec
Reputation Power: 379
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
Then I compiled and opened the assembly in Reflector and viewed the C# version, which goes like this:
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);
Then I found this article. Search for "UnmanagedType.VBByRefStr" on the page, and read some of the stuff in the section called Learn the Ins and Outs. It turns out that VB.NET automatically changes ByVal argname As String to something completely different. You may have to MarshalAs(UnmanagedType.LPTStr) instead, or you may simple need to have both as ref arguments.

Reply With Quote
  #6  
Old April 30th, 2008, 10:38 AM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,610 LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 18 h 39 m 36 sec
Reputation Power: 379
Please read more here and here.

First try making both ref parameters. The default marshaling is as UnmanagedType.BStr. If that doesn't work, you can try UnmanagedType.LPTStr or UnmanagedType.VBByRefStr.

Just add one piece at a time until it works.

Reply With Quote
  #7  
Old May 5th, 2008, 10:23 PM
stanley1610's Avatar
stanley1610 stanley1610 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 250 stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 16 h 15 m 52 sec
Reputation Power: 8
Thumbs up

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:
Originally Posted by LyonHaert
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
Then I compiled and opened the assembly in Reflector and viewed the C# version, which goes like this:
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);
Then I found this article. Search for "UnmanagedType.VBByRefStr" on the page, and read some of the stuff in the section called Learn the Ins and Outs. It turns out that VB.NET automatically changes ByVal argname As String to something completely different. You may have to MarshalAs(UnmanagedType.LPTStr) instead, or you may simple need to have both as ref arguments.

Reply With Quote
  #8  
Old May 6th, 2008, 09:47 AM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,610 LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 18 h 39 m 36 sec
Reputation Power: 379
Have you tried assigning it to a new [normal] String variable?

Reply With Quote
  #9  
Old May 8th, 2008, 12:34 AM
stanley1610's Avatar
stanley1610 stanley1610 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 250 stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level)stanley1610 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 16 h 15 m 52 sec
Reputation Power: 8
Quote:
Originally Posted by LyonHaert
Have you tried assigning it to a new [normal] String variable?


strGMW_DB_Read_Buf.SubString(0, 20).Trim();

Reply With Quote
  #10  
Old May 8th, 2008, 09:52 AM
LyonHaert's Avatar
LyonHaert LyonHaert is offline
Arcane Scribbler
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2005
Location: Indianapolis, IN
Posts: 1,610 LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level)LyonHaert User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 18 h 39 m 36 sec
Reputation Power: 379
Quote:
Originally Posted by stanley1610
strGMW_DB_Read_Buf.SubString(0, 20).Trim();
So you added a call to Substring(). Did it work, did it not work? I'm not telepathic.

Reply With Quote
  #11  
Old May 8th, 2008, 10:14 AM
f'lar's Avatar
f'lar f'lar is offline
Senior WeyrLeader
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Aug 2003
Location: WI
Posts: 3,782 f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 22 h 12 m 27 sec
Reputation Power: 675
Send a message via Google Talk to f'lar
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]

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > Convert VB.NET To C#.NET


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump