473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Couls someone translate VB->C# ?

Could someone please translate the code below into C#?
Please also tell me the libraries I might need.

Many thanks,
Adrian.
int main()
{
(GetProcAddress( LoadLibrary( "krnl386.exe" ), "exitkernel" ))();
return( 0 );
}
Sep 6 '07 #1
11 1770
On Sep 6, 9:12 am, "Adrian" <x...@xx.xxwrote:
Could someone please translate the code below into C#?
Please also tell me the libraries I might need.

Many thanks,
Adrian.

int main()
{
(GetProcAddress( LoadLibrary( "krnl386.exe" ), "exitkernel" ))();
return( 0 );
}
First, this is C, not VB. Does this work if you compile with a C
compiler? Why do you NEED to have it in C#?

You need to add the appropriate DllImport methods. THe "standard" way
would be something like:

[DllImport("kernel32.dll", CallingConvention =
CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError =
true)]
static extern IntPtr LoadLibrary(String lpFileName);
[DllImport("kernel32.dll", CallingConvention =
CallingConvention.Winapi, CharSet = CharSet.Ansi, SetLastError =
true)]
static extern IntPtr GetProcAddress(IntPtr hModule, String
lpEntryPoint);

Then you need to add a delegate definition so that you can call the
function returned from GetProcAddress.
IMPORTANT NOTE: I believe that you can ONLY convert __stdcall function
pointers into delegates.
Something like:

delegate uint GetWindowsDirectory([MarshalAs(UnmanagedType.LPStr)]
StringBuilder lpBuffer, uint uSize);
const uint MAX_PATH = 260;

Then you need to call the functions, something like this:
static void Main(string[] args)
{
// Get the dll you need to load. Note that krnl386.exe doesn't
want to load this way.
IntPtr hLib = LoadLibrary("kernel32.dll");
if (hLib == IntPtr.Zero)
throw new Win32Exception();

// Get the procedure address.
IntPtr procAddress = GetProcAddress(hLib,
"GetWindowsDirectoryA");
if (procAddress == IntPtr.Zero)
throw new Win32Exception();

// Convert it into a delgate.
GetWindowsDirectory getWinDir =
(GetWindowsDirectory)Marshal.GetDelegateForFunctio nPointer(procAddress,
typeof(GetWindowsDirectory));
// Set up parameters
StringBuilder winDir = new StringBuilder((int)MAX_PATH);
// Call the delegate (which calls the function pointer)
uint retval = getWinDir(winDir, MAX_PATH);
if (retval == 0)
throw new Exception("Cannot determine error on non dll
import functions...");
Console.WriteLine(winDir);
}

Sep 6 '07 #2
Hello, Adrian!

Maybe, something like this?

[DllImport("krnl386.exe", EntryPoint="exitkernel", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern void exitkernel();

int main()
{
exitkernel();
return (0);
}
--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Thu, 6 Sep 2007 15:12:04 +0200:

ACould someone please translate the code below into C#?
APlease also tell me the libraries I might need.

AMany thanks,
AAdrian.
Aint main()
A{
A (GetProcAddress( LoadLibrary( "krnl386.exe" ), "exitkernel" ))();
A return( 0 );
A}


Sep 6 '07 #3
Hi,
That is C, Do you know what that code does?
What are you trying to do?
"Adrian" <xx@xx.xxwrote in message
news:46**********************@news.tiscali.nl...
Could someone please translate the code below into C#?
Please also tell me the libraries I might need.

Many thanks,
Adrian.
int main()
{
(GetProcAddress( LoadLibrary( "krnl386.exe" ), "exitkernel" ))();
return( 0 );
}

Sep 6 '07 #4
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:Oz****************@TK2MSFTNGP05.phx.gbl...
Hi,
That is C, Do you know what that code does?
What are you trying to do?
"Adrian" <xx@xx.xxwrote in message
news:46**********************@news.tiscali.nl...
>Could someone please translate the code below into C#?
Please also tell me the libraries I might need.

Many thanks,
Adrian.
int main()
{
(GetProcAddress( LoadLibrary( "krnl386.exe" ), "exitkernel" ))();
return( 0 );
}
I am attempting to close down a W98 box from a non-W98 box in a network.
Someone was kind enough to give me the code in VB and I wanted to know what
the code would be in C# so I could try it.

Adrian.
Sep 6 '07 #5
On Sep 6, 2:43 pm, "Adrian" <x...@xx.xxwrote:
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
messagenews:Oz****************@TK2MSFTNGP05.phx.gb l...


Hi,
That is C, Do you know what that code does?
What are you trying to do?
"Adrian" <x...@xx.xxwrote in message
news:46**********************@news.tiscali.nl...
Could someone please translate the code below into C#?
Please also tell me the libraries I might need.
Many thanks,
Adrian.
int main()
{
(GetProcAddress( LoadLibrary( "krnl386.exe" ), "exitkernel" ))();
return( 0 );
}

I am attempting to close down a W98 box from a non-W98 box in a network.
Someone was kind enough to give me the code in VB and I wanted to know what
the code would be in C# so I could try it.

Adrian
Until you said Win98 box, I would have suggested using the Shutdown
Method of the Win32_OperatingSystem WMI Class <g>

Sep 6 '07 #6

"Doug Semler" <do********@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
Until you said Win98 box, I would have suggested using the Shutdown
Method of the Win32_OperatingSystem WMI Class <g>
Thank you Doug.
Adrian.
Sep 6 '07 #7
"Stetsiak" <va*****@gmail.comwrote in message
news:Ok**************@TK2MSFTNGP03.phx.gbl...
Hello, Adrian!

Maybe, something like this?

[DllImport("krnl386.exe", EntryPoint="exitkernel", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern void exitkernel();

int main()
{
exitkernel();
return (0);
}
Thank you Vadym.
Adrian.
Sep 6 '07 #8
Re your code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ShutDownW98
{
class ShutDown
{
[DllImport("krnl386.exe", EntryPoint="exitkernel", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern void exitkernel();
static void Main(string[] args)
{
exitkernel();
}
}
}
Produced this error:
AppName: sutdownw98.exe AppVer: 1.0.0.0 AppStamp:46e11bdc
ModName: kernel32.dll ModVer: 5.1.2600.3119 ModStamp:46239bd5
fDebug: 0 Offset: 00012a5b

Adrian.
Sep 7 '07 #9
Hello, Adrian!

Was this error obtained when code called exitkernel or when application had
just started?

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com
You wrote on Fri, 7 Sep 2007 11:48:05 +0200:

ARe your code:

Ausing System;
Ausing System.Collections.Generic;
Ausing System.Text;
Ausing System.Runtime.InteropServices;
Anamespace ShutDownW98 {
Aclass ShutDown {
A[DllImport("krnl386.exe", EntryPoint="exitkernel", SetLastError=true,
ACharSet=CharSet.Unicode, ExactSpelling=true,
ACallingConvention=CallingConvention.StdCall)]
Apublic static extern void exitkernel();
Astatic void Main(string[] args)
A{
Aexitkernel();
A}
A}
A}
AProduced this error:
AAppName: sutdownw98.exe AppVer: 1.0.0.0 AppStamp:46e11bdc
AModName: kernel32.dll ModVer: 5.1.2600.3119
AModStamp:46239bd5 fDebug: 0 Offset: 00012a5b

AAdrian.


Sep 7 '07 #10
"Doug Semler" <do********@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
<snipped>
In other words, while it is possible to do this, I don't think you can
do it it unless there's an interactive session active. And if there
is a session active, you have to ask yourself if you REALLY want to
pull the rug out from under someone who may lose their work...
It has been done using a simple looping bat file, a clever someone suggested
to me. As to pulling the rug from underneath someone: I use the W98 box as a
data store, accessible throughout the network. So the rug-issue isn't in
play here. Thank you though, Adrian.

:Again
if exist C:\Share\Stop.txt goto Execute

rem Wait for 10 seconds
ping -n 10 127.0.0.2 nul
goto Again

:Execute
del C:\Share\Stop.txt
rundll32 krnl386.exe,exitkernel
goto Again

Sep 7 '07 #11
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:OM*************@TK2MSFTNGP04.phx.gbl...
Here is how you can use this API, but this is the least of your problem,
you must run this on the remote Win98 system, that means that you should
implement a "remoting call" over the network.

[DllImport("user32.dll", EntryPoint="ExitWindowsEx ", SetLastError=true)]
static extern int ExitWindows(uint flags, int reason);

...
const int poweroff = 8;
const int shutdown = 1;
int win32code;
if((win32code = ExitWindows(poweroff , 0)) == 0)
Console.WriteLine("Failed with - Error code: {0}", win32code );

Willy.
Willy, Thank you. Please see my response to Doug. I have been pursuing the
search for a solution in C#, because that is my language, however, the
bat-file solution proves to be simpler. Regards, Adrian.
Sep 7 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JoRo | last post by:
I'm working on a project that needs this code in C#. Unfortunately I'm not very familiar with VB. Can anyone help translate the following into C#? Class myHyperlinkColumn Implements ITemplate...
4
by: cloudx | last post by:
hi there, how do I translate the following vb code to C#? I know to use Indexof, but what would be for """"? Thanks! InStr(1, sDocName, """")
4
by: Harry Hudini | last post by:
Hi, I need to run exactly this code in an asp.net file when someone uploads an image, but i dont know C# and im having real issues converting it. If anyone can, could you convert it to VB.net...
6
by: Carlos | last post by:
Hi all, ca anybody help me to translate to vb .net this c# instruction? string fname = ((TextBox)GridView1.Rows. Cells.Controls).Text; Thanks, Carlos
3
by: Mike | last post by:
public class Broadcaster: MarshalByRefObject, IBroadcaster { public event General.MessageArrivedHandler MessageArrived; public void BroadcastMessage(string msg) { Console.WriteLine("Will...
9
bvdet
by: bvdet | last post by:
I have done some more work on a simple class I wrote to calculate a global coordinate in 3D given a local coordinate: ## Basis3D.py Version 1.02 (module macrolib.Basis3D) ## Copyright (c) 2006...
4
by: dancer | last post by:
Who can translate this code into VB.net? On May 19, 4:48 am, "dancer" <dan...@microsoft.comwrote: Hi.... here it goes
3
by: raz230 | last post by:
I'm sorry for posting this here- I have to integrate an parcel tracking with Canada Post into one of my applications. The web service they use is made with SAP and is not the usual WSDL type of...
2
by: is_vlb50 | last post by:
I have problem with translate next code from c# to vb : private UStateEventHandler _myUStateEventHandler; //where UStateEventHandler is defined as : //public sealed delegate...
3
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I've just been tasked to translate a C# project into VB (Dot Net 2.0). I am somewhat familiar with C# but far from an expert. I'd like a second opinion on this one. The project uses CSLA...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.