Couls someone translate VB->C# ?

A

Adrian

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 );
}
 
D

Doug Semler

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.GetDelegateForFunctionPointer(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);
}
 
V

Vadym Stetsiak

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:

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

A> Many thanks,
A> Adrian.


A> int main()
A> {
A> (GetProcAddress( LoadLibrary( "krnl386.exe" ), "exitkernel" ))();
A> return( 0 );
A> }
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


That is C, Do you know what that code does?
What are you trying to do?
 
A

Adrian

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,


That is C, Do you know what that code does?
What are you trying to do?

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

Doug Semler

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.com> wrote in
message







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

Adrian

Doug Semler said:
Until you said Win98 box, I would have suggested using the Shutdown
Method of the Win32_OperatingSystem WMI Class <g>
Thank you Doug.
Adrian.
 
A

Adrian

Stetsiak said:
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.
 
A

Adrian

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

Vadym Stetsiak

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:

A> Re your code:

A> using System;
A> using System.Collections.Generic;
A> using System.Text;
A> using System.Runtime.InteropServices;
A> namespace ShutDownW98 {
A> class ShutDown {
A> [DllImport("krnl386.exe", EntryPoint="exitkernel", SetLastError=true,
A> CharSet=CharSet.Unicode, ExactSpelling=true,
A> CallingConvention=CallingConvention.StdCall)]
A> public static extern void exitkernel();
A> static void Main(string[] args)
A> {
A> exitkernel();
A> }
A> }
A> }


A> Produced this error:
A> AppName: sutdownw98.exe AppVer: 1.0.0.0 AppStamp:46e11bdc
A> ModName: kernel32.dll ModVer: 5.1.2600.3119
A> ModStamp:46239bd5 fDebug: 0 Offset: 00012a5b

A> Adrian.
 
W

Willy Denoyette [MVP]

Adrian said:
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.


You did not expect to shutdown a remote Win98 machine by running this on XP
did you? You can only run this on Win98, and this only to shutdown the local
(Win98) machine.

Some further remarks:
- Don't call undocumented API's like exitkernel, these are not supposed to
be portable.
- Use ExitWindowsEx from User32.dll in order to shutdown Win98 and higher.
- You can't shutdown a remote Win98 system directly using any of the
available API's, that means that you will have to implement a Client/Server
application where the Server (Win98) calls "ExitWindowsEx " when requested
by the remote Client(s).

Willy.
 
A

Adrian

Willy Denoyette said:
Adrian said:
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.


You did not expect to shutdown a remote Win98 machine by running this on
XP did you? You can only run this on Win98, and this only to shutdown the
local (Win98) machine.

Some further remarks:
- Don't call undocumented API's like exitkernel, these are not supposed to
be portable.
- Use ExitWindowsEx from User32.dll in order to shutdown Win98 and higher.
- You can't shutdown a remote Win98 system directly using any of the
available API's, that means that you will have to implement a
Client/Server application where the Server (Win98) calls "ExitWindowsEx "
when requested by the remote Client(s).

Willy.
I want to close down the W98 box from another non-W98 box. How do I
implement your suggestion? Re: "Use ExitWindowsEx from User32.dll in order
to shutdown Win98 and higher." What code should be substituted? (I am not
into this sort of coding.) Thanks, Adrian.
 
W

Willy Denoyette [MVP]

Adrian said:
Willy Denoyette said:
Adrian said:
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.


You did not expect to shutdown a remote Win98 machine by running this on
XP did you? You can only run this on Win98, and this only to shutdown the
local (Win98) machine.

Some further remarks:
- Don't call undocumented API's like exitkernel, these are not supposed
to be portable.
- Use ExitWindowsEx from User32.dll in order to shutdown Win98 and
higher.
- You can't shutdown a remote Win98 system directly using any of the
available API's, that means that you will have to implement a
Client/Server application where the Server (Win98) calls "ExitWindowsEx "
when requested by the remote Client(s).

Willy.
I want to close down the W98 box from another non-W98 box. How do I
implement your suggestion? Re: "Use ExitWindowsEx from User32.dll in order
to shutdown Win98 and higher." What code should be substituted? (I am not
into this sort of coding.) Thanks, Adrian.


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

Doug Semler

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

You did not expect to shutdown a remote Win98 machine by running this on XP
did you? You can only run this on Win98, and this only to shutdown the local
(Win98) machine.

Some further remarks:
- Don't call undocumented API's like exitkernel, these are not supposed to
be portable.
- Use ExitWindowsEx from User32.dll in order to shutdown Win98 and higher.
- You can't shutdown a remote Win98 system directly using any of the
available API's, that means that you will have to implement a Client/Server
application where the Server (Win98) calls "ExitWindowsEx " when requested
by the remote Client(s).

Willy.

One further remark, the Server has to be running in the interactive
user's session when ExitWindowsEx is called...And since Win98 boxes
don't have services (IIRC), you won't be able to run this unless
someone is actually logged on to the Win98 box. Personally, I would
not like it if my computer shut down while I was working on it. (We
have one of those things at work when critical updates are pushed to
the domain pc, and EVERYBODY hates it because it has a countdown timer
and the timer tends to expire in the middle of the day).

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

Adrian

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
 
A

Adrian

Willy Denoyette said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top