Computing.Net > Forums > Programming > Question Regarding VB.net and C#

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Question Regarding VB.net and C#

Reply to Message Icon

Name: Yoshi
Date: September 23, 2002 at 17:07:44 Pacific
OS: XP Pro
CPU/Ram: 800mhz 256Meg
Comment:

I have this program I wrote in VB.net. Basicly the program is messager that uses NTs Net Send service to send a message to a computer on a LAN.
I want to take this program I have in VB code and port it into C#. I need the code in C# because I want to use this program as a Case Study for my C# class.
My problem is I'm not all that sure on the C# syntax for
Private Declare Function NetMessageBufferSend Lib _
"NETAPI32.DLL" (yServer As Any, yToName As Byte, _
yFromName As Any, yMsg As Byte, ByVal lSize As Long) As Long

Public Function BroadcastMessage(sToUser As String, _
sFromUser As String, sMessage As String) As Boolean

Dim yToName() As Byte
Dim yFromName() As Byte
Dim yMsg() As Byte
Dim l As Long

yToName = sToUser & vbNullChar
yFromName = sFromUser & vbNullChar
yMsg = sMessage & vbNullChar

If anyone knows how to do this sort of thing in C# please tell me.
I'm going to have a hard enough time learning C# Forms(yeah that's right We are not going to cover Forms in C# just stupid console apps. I had enough console apps from C.)



Sponsored Link
Ads by Google

Response Number 1
Name: Yoshi
Date: September 23, 2002 at 17:10:30 Pacific
Reply:

Opps. that Code was copyed from the Old VB6 version. The "Anys" were changed to bytes in my VB.net Version


0

Response Number 2
Name: Jeff J
Date: September 23, 2002 at 20:07:48 Pacific
Reply:

C# is being taught in classes already!? I will never get used to the relentless pace of technology.

The new .Net languages equivalent for "Declare Function" is DllImport, although the former has been kept for backwards compatibility with VB (it will likely be deprecated in the next Visual Studio release). DllImport is much more flexible, and is essentially a replacement for COM's IDL, which was used to create Type Libraries (though few did for VB). Here is an example:

//Given this C-style prototype
int DoStuff(LPCWSTR szPath, DWORD dwFlags, DWORD *pAttr,
         LPBYTE bytBuf, DWORD dwBufLen);

//Imported via C# (or VB.Net with minor changes)
using System.Runtime.InteropServices;

[
DllImport
 "Some.dll", EntryPoint="DoStuff",
 ExactSpelling=true, CharSet=CharSet.Unicode

 public extern static int
 DoStuff([MarshalAs(UnmanagedType.LPWStr)] string pzPath,
            uint dwFlags,
            ref uint pAttr,
            [MarshalAs(UnmanagedType.LPArray,
                    ArraySubType=UnmanagedType.U1,
                    SizeParamIndex=4)] byte[] bytBuf,
            uint dwBufLen);
]

Simple, isn't it? I've even considered writing a converter, but there are some things that defy automatic detection, and require a human brain (it's still useful in programming to a degree ;). All of the types you will need for your code are covered here, but you might want to lookup specifics in .Net help or online at msdn.microsoft.com.

As you know, "As Any" (a void pointer) is no longer used nor needed. Array and string passing are far superior to VB5/6, since no back-and-forth marshaling and translations are used (MUCH faster). That is, parameter passing is more or less direct, within a non-garbage-collected memory area. Such are the advantages of being directly compatible with C/C++ (if only C# could be as terse).

Note that the keyword "ref" really means a pointer (modern languages refuse to admit they must exist). Null-terminated strings do not need to be declared as array types, just whether wide, narrow, etc. The last two params are for a regular array, where ArraySubType (element type) is set to unsigned byte (unsigned char), and the SizeParamIndex (the param that holds the array's size) is zero-based (fifth param, or index of 4, which is dwBufLen).

Good luck with your project!

WWWWWWWWWWWWWW_WidthEnforcer_WWWWWWWWWWWWWW


0

Response Number 3
Name: Yoshi
Date: September 24, 2002 at 07:25:50 Pacific
Reply:

Thanks. I'll try that as soon as I get a chance.

It sucks not having VS.Net on my Home PC(I do have VS 6 though but a lot of good that does me for my VB.net and C# classes)


Oh BTW, This is the first term my school is teaching .NET(and my teacher isnt up to speed, but he couldn't teach C++ well. Heck he never even went over how to handle forms in VB.net which is a huge change from VB6. Another thing that sucks is having a C class and a VB class back to back.)


0

Response Number 4
Name: Jeff J
Date: September 24, 2002 at 09:28:49 Pacific
Reply:

Thank you very much for the feedback; I always like to know what's really going on in the world, beyond the usual public hype and marketing campaigns.

FWIW, academia wasn't any better in my day (when we walked to school uphill both ways ;). Some professors like teaching, and the others are horrid at it. Just take what you can from the situation, and when you can, find some good mentors in the working world.

Best of luck...

"Few are those who see with their own eyes and feel with their own hearts." -- Einstein

"The most uncommon thing is common sense." -- unknown


0

Response Number 5
Name: Yoshi
Date: September 24, 2002 at 12:23:36 Pacific
Reply:

I have mixed feelings with .NET. The theory behind .NET sounds goods. You make a program in your favorate language, C++,VB,Cobal(yes they have Cobal addins as well as Fortran for .NET) you compile your program once and it can be used on any machine with the .net framework(much like Java's VM). The crux is, Microsoft is never going to provide .Net Framework for Linux,Mac, or any non-MS operating systems, so I think it would be better to compile directly in to machine code instead of the intermedite language(A option to choose between the native code or MIL would be nice). It does not make much sence to require a the 25meg framework to run a 54kilobyte program that was written in VB.net


0

Related Posts

See More



Response Number 6
Name: DanyOlgiax
Date: September 26, 2002 at 00:56:29 Pacific
Reply:

HELP!

i try this code but it doesn't works in vb.net!!

(the error is in '!!!!!error' lines!)

thanks!

Private Declare Function NetMessageBufferSend Lib _
"NETAPI32.DLL" (yServer As Any, yToName As Byte, _
yFromName As Any, yMsg As Byte, ByVal lSize As Long) As Long
Public Function BroadcastMessage(sToUser As String, _
sFromUser As String, sMessage As String) As Boolean

Dim yToName() As Byte
Dim yFromName() As Byte
Dim yMsg() As Byte
Dim l As Long


yToName = sToUser & vbNullChar '!!!!!!!!!! error
yFromName = sFromUser & vbNullChar '!!!!!!! error

yMsg = sMessage & vbNullChar '!!!!!!! error


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Question Regarding VB.net and C#

VB.net vs C#.net www.computing.net/answers/programming/vbnet-vs-cnet/3930.html

VB.net or C#? www.computing.net/answers/programming/vbnet-or-c/13972.html

VB.NET and C DLL's www.computing.net/answers/programming/vbnet-and-c-dlls/9509.html