Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 LongPublic Function BroadcastMessage(sToUser As String, _
sFromUser As String, sMessage As String) As BooleanDim yToName() As Byte
Dim yFromName() As Byte
Dim yMsg() As Byte
Dim l As LongyToName = sToUser & vbNullChar
yFromName = sFromUser & vbNullChar
yMsg = sMessage & vbNullCharIf 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.)

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

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

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

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

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

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 BooleanDim yToName() As Byte
Dim yFromName() As Byte
Dim yMsg() As Byte
Dim l As Long
yToName = sToUser & vbNullChar '!!!!!!!!!! error
yFromName = sFromUser & vbNullChar '!!!!!!! erroryMsg = sMessage & vbNullChar '!!!!!!! error

![]() |
![]() |
![]() |

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