473,725 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ex e" ), "exitkernel " ))();
return( 0 );
}
Sep 6 '07 #1
11 1792
On Sep 6, 9:12 am, "Adrian" <x...@xx.xxwrot e:
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.ex e" ), "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("kern el32.dll", CallingConventi on =
CallingConventi on.Winapi, CharSet = CharSet.Auto, SetLastError =
true)]
static extern IntPtr LoadLibrary(Str ing lpFileName);
[DllImport("kern el32.dll", CallingConventi on =
CallingConventi on.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 GetWindowsDirec tory([MarshalAs(Unman agedType.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("ke rnel32.dll");
if (hLib == IntPtr.Zero)
throw new Win32Exception( );

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

// Convert it into a delgate.
GetWindowsDirec tory getWinDir =
(GetWindowsDire ctory)Marshal.G etDelegateForFu nctionPointer(p rocAddress,
typeof(GetWindo wsDirectory));
// Set up parameters
StringBuilder winDir = new StringBuilder(( int)MAX_PATH);
// Call the delegate (which calls the function pointer)
uint retval = getWinDir(winDi r, MAX_PATH);
if (retval == 0)
throw new Exception("Cann ot determine error on non dll
import functions...");
Console.WriteLi ne(winDir);
}

Sep 6 '07 #2
Hello, Adrian!

Maybe, something like this?

[DllImport("krnl 386.exe", EntryPoint="exi tkernel", SetLastError=tr ue,
CharSet=CharSet .Unicode, ExactSpelling=t rue,
CallingConventi on=CallingConve ntion.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.ex e" ), "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.xxwrot e 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.ex e" ), "exitkernel " ))();
return( 0 );
}

Sep 6 '07 #4
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions .comwrote in
message news:Oz******** ********@TK2MSF TNGP05.phx.gbl. ..
Hi,
That is C, Do you know what that code does?
What are you trying to do?
"Adrian" <xx@xx.xxwrot e 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.ex e" ), "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.xxwrot e:
"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions .comwrote in
messagenews:Oz* *************** @TK2MSFTNGP05.p hx.gbl...


Hi,
That is C, Do you know what that code does?
What are you trying to do?
"Adrian" <x...@xx.xxwrot e 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.ex e" ), "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_Operating System WMI Class <g>

Sep 6 '07 #6

"Doug Semler" <do********@gma il.comwrote in message
news:11******** *************@w 3g2000hsg.googl egroups.com...
Until you said Win98 box, I would have suggested using the Shutdown
Method of the Win32_Operating System WMI Class <g>
Thank you Doug.
Adrian.
Sep 6 '07 #7
"Stetsiak" <va*****@gmail. comwrote in message
news:Ok******** ******@TK2MSFTN GP03.phx.gbl...
Hello, Adrian!

Maybe, something like this?

[DllImport("krnl 386.exe", EntryPoint="exi tkernel", SetLastError=tr ue,
CharSet=CharSet .Unicode, ExactSpelling=t rue,
CallingConventi on=CallingConve ntion.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.Collecti ons.Generic;
using System.Text;
using System.Runtime. InteropServices ;
namespace ShutDownW98
{
class ShutDown
{
[DllImport("krnl 386.exe", EntryPoint="exi tkernel", SetLastError=tr ue,
CharSet=CharSet .Unicode, ExactSpelling=t rue,
CallingConventi on=CallingConve ntion.StdCall)]
public static extern void exitkernel();
static void Main(string[] args)
{
exitkernel();
}
}
}
Produced this error:
AppName: sutdownw98.exe AppVer: 1.0.0.0 AppStamp:46e11b dc
ModName: kernel32.dll ModVer: 5.1.2600.3119 ModStamp:46239b d5
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.Collecti ons.Generic;
Ausing System.Text;
Ausing System.Runtime. InteropServices ;
Anamespace ShutDownW98 {
Aclass ShutDown {
A[DllImport("krnl 386.exe", EntryPoint="exi tkernel", SetLastError=tr ue,
ACharSet=CharSe t.Unicode, ExactSpelling=t rue,
ACallingConvent ion=CallingConv ention.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:46e11b dc
AModName: kernel32.dll ModVer: 5.1.2600.3119
AModStamp:46239 bd5 fDebug: 0 Offset: 00012a5b

AAdrian.


Sep 7 '07 #10

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

Similar topics

2
5527
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 Sub instantiatein(ByVal container As Control) Implements ITemplate.InstantiateIn Dim hlink As HyperLink = New HyperLink AddHandler hlink.DataBinding, AddressOf BindHyperLinkColumn
4
1962
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
2417
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 for me to use ? Loads of karma in it for you :) <%@ Import Namespace="System.Collections" %>
6
1421
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
2787
by: Mike | last post by:
public class Broadcaster: MarshalByRefObject, IBroadcaster { public event General.MessageArrivedHandler MessageArrived; public void BroadcastMessage(string msg) { Console.WriteLine("Will broadcast message: {0}", msg); SafeInvokeEvent(msg); }
9
3109
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 Bruce Vaughan, BV Detailing & Design, Inc. ## All rights reserved. ## NOT FOR SALE. The software is provided "as is" without any warranty. ############################################################################ """ Class...
4
1720
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
1529
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 service I am used to. I cannot just add a web reference to my project and get going. Canada Post provides a sample in Java and C# - and I do not know either.
2
880
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 UStateEventHandler : System.MulticastDelegate // Create the event handlers we need.
3
1265
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 2.1.4. The project has a class that inherits from ReadOnlyBase. ReadOnlyBase has a method declaration: protected virtual void DataPortal_Fetch(object criteria) { throw new NotSupportedException(Resources.FetchNotSupportedException);
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.