Home All Groups Group Topic Archive Search About

Help in translating C to VB

Author
24 Feb 2007 1:43 PM
Joris van Lier
Hi all, i found some C code that exactly does what i need to: turn on or off the email led on my acer laptop,
actually it allmost does what i need to, because i need to do that in VB6,
would you be so kind to give me some pointers on how to convert the declarations to VB6 style?

Note: i'm not asking to translate this for me, i'd like to do that on my own thank you, as i hope it will increase my understanding of VB6 and C

Credits for the C version go to Grimpy  http://web.archive.org/web/20051218031656/gradict.kahosl.be/jo.deboeck/progs/acerled.zip
********************
This is the C version, further down you'll find my attemt at translating it to VB with C interspersed
********************
/*
Acer mail led  by Grimpy

*/

#include <windows.h>
#include <shlobj.h>

HRESULT WINAPI SHGetUnreadMailCountW(
void *hKeyUser,
void *MailAddress,
void *count,
FILETIME *stamp,
void *Shellcommand,
int doShell);

HANDLE ATKACPIhandle;

int CtrlACPI(int onoff) //voor off 0 en voor on 1
{
long bytes = 0;
long inbuf[5];
struct cmbuf {
  short cmds[2];
  long cm2;
} cbuf;
long outbuf[192];
int ret;
inbuf[0] = onoff;
ret = DeviceIoControl(ATKACPIhandle, 0x222410, inbuf, sizeof(inbuf),
  outbuf, sizeof(outbuf), &bytes, NULL);
return ret;
}
main()
{
FILETIME stamp = {0};
long old = 0, count;
ATKACPIhandle = CreateFile("\\\\.\\HOTKEY", GENERIC_READ|GENERIC_WRITE,
  FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
  NULL);
if (!ATKACPIhandle) {
  MessageBox(NULL,"Fout bij laden vor driver","ERROR",MB_ICONERROR);
  exit(0);
}
CtrlACPI(1);
}

**************************************
My attempt at VB translation
**************************************

Public Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type


Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const FILE_SHARE_READ = &H1
Public Const FILE_SHARE_WRITE = &H2
Public Const OPEN_EXISTING = 3

Public Type OVERLAPPED
        Internal As Long
        InternalHigh As Long
        offset As Long
        OffsetHigh As Long
        hEvent As Long
End Type

Public Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type

Public Declare Function DeviceIoControl Lib "kernel32" ( _
    ByVal hDevice As Long, _
    ByVal dwIoControlCode As Long, _
    lpInBuffer As Any, _
    ByVal nInBufferSize As Long, _
    lpOutBuffer As Any, _
    ByVal nOutBufferSize As Long, _
    lpBytesReturned As Long, _
    lpOverlapped As OVERLAPPED _
) As Long

Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _
    ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As Long _
) As Long
'C: int CtrlACPI(int onoff) //voor off 0 en voor on 1
Public Function CtrlACPI(onoff As Integer) As Integer
    'C:  long bytes = 0;
    Dim bytes As Long: bytes = 0
    'C:  long inbuf[5];
    Dim inbuf(5) As Long
    'C: struct cmbuf {
    'C:  short cmds[2];
    'C:  long cm2;
    'C: } cbuf;
    '* Here i need some help, how do i declare that struct?
    ' TODO: Declaration of struct cmbuf


    'C:  long outbuf[192];
    Dim outbuf(192) As Long
    'C: int ret;
    Dim ret As Integer
    'C: inbuf[0] = onoff;
    inbuf(0) = onoff
    'C: ret = DeviceIoControl(ATKACPIhandle, 0x222410, inbuf, sizeof(inbuf),
    'C: outbuf, sizeof(outbuf), &bytes, NULL);
    ret = DeviceIoControl(ATKACPIhandle, &H222410, inbuf, UBound(inbuf), _
    outbuf, UBound(outbuf), bytes, Null)
    'C: return ret;
    CtrlACPI = ret
End Function

'C: main() {
Sub Main()
    'C:  FILETIME stamp = {0};
    Dim stamp As FILETIME: stamp = 0
    'C:  long old = 0, count;
    Dim old As Long: old = 0
    Dim count As Long
    'C: ATKACPIhandle = CreateFile("\\\\.\\HOTKEY", GENERIC_READ|GENERIC_WRITE,
    'C:  FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
    'C:  NULL);
    Dim ATKACPIhandle As Long
    ATKACPIhandle = CreateFile("\\\\.\\HOTKEY", GENERIC_READ Or GENERIC_WRITE, _
        FILE_SHARE_READ Or FILE_SHARE_WRITE, Null, OPEN_EXISTING, 0, _
        Null)
    'C: if (!ATKACPIhandle) {
    If (ATKACPIhandle = 0) Then
        'C: MessageBox(NULL,"Fout bij laden vor driver","ERROR",MB_ICONERROR);
        MsgBox "Error loading driver", vbCritical, "ERROR"
        'C: exit(0);
    'C: }
    'C: CtrlACPI(1);
    Call CtrlACPI(1)
End Sub

Author
24 Feb 2007 3:57 PM
Galen Somerville
Go to PlanetSourceCode and look for C2VB.

Galen

"Joris van Lier" <whiz***@hotmail.com> wrote in message
news:uCP$SnBWHHA.4632@TK2MSFTNGP04.phx.gbl...
Hi all, i found some C code that exactly does what i need to: turn on or off the email led on my
acer laptop,
actually it allmost does what i need to, because i need to do that in VB6,
would you be so kind to give me some pointers on how to convert the declarations to VB6 style?

Note: i'm not asking to translate this for me, i'd like to do that on my own thank you, as i hope it
will increase my understanding of VB6 and C

Credits for the C version go to Grimpy
http://web.archive.org/web/20051218031656/gradict.kahosl.be/jo.deboeck/progs/acerled.zip
********************
This is the C version, further down you'll find my attemt at translating it to VB with C
interspersed
********************
/*
Acer mail led  by Grimpy

*/

#include <windows.h>
#include <shlobj.h>

HRESULT WINAPI SHGetUnreadMailCountW(
void *hKeyUser,
void *MailAddress,
void *count,
FILETIME *stamp,
void *Shellcommand,
int doShell);

HANDLE ATKACPIhandle;

int CtrlACPI(int onoff) //voor off 0 en voor on 1
{
long bytes = 0;
long inbuf[5];
struct cmbuf {
  short cmds[2];
  long cm2;
} cbuf;
long outbuf[192];
int ret;
inbuf[0] = onoff;
ret = DeviceIoControl(ATKACPIhandle, 0x222410, inbuf, sizeof(inbuf),
  outbuf, sizeof(outbuf), &bytes, NULL);
return ret;
}
main()
{
FILETIME stamp = {0};
long old = 0, count;
ATKACPIhandle = CreateFile("\\\\.\\HOTKEY", GENERIC_READ|GENERIC_WRITE,
  FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
  NULL);
if (!ATKACPIhandle) {
  MessageBox(NULL,"Fout bij laden vor driver","ERROR",MB_ICONERROR);
  exit(0);
}
CtrlACPI(1);
}

**************************************
My attempt at VB translation
**************************************

Public Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type


Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const FILE_SHARE_READ = &H1
Public Const FILE_SHARE_WRITE = &H2
Public Const OPEN_EXISTING = 3

Public Type OVERLAPPED
        Internal As Long
        InternalHigh As Long
        offset As Long
        OffsetHigh As Long
        hEvent As Long
End Type

Public Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type

Public Declare Function DeviceIoControl Lib "kernel32" ( _
    ByVal hDevice As Long, _
    ByVal dwIoControlCode As Long, _
    lpInBuffer As Any, _
    ByVal nInBufferSize As Long, _
    lpOutBuffer As Any, _
    ByVal nOutBufferSize As Long, _
    lpBytesReturned As Long, _
    lpOverlapped As OVERLAPPED _
) As Long

Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _
    ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As Long _
) As Long
'C: int CtrlACPI(int onoff) //voor off 0 en voor on 1
Public Function CtrlACPI(onoff As Integer) As Integer
    'C:  long bytes = 0;
    Dim bytes As Long: bytes = 0
    'C:  long inbuf[5];
    Dim inbuf(5) As Long
    'C: struct cmbuf {
    'C:  short cmds[2];
    'C:  long cm2;
    'C: } cbuf;
    '* Here i need some help, how do i declare that struct?
    ' TODO: Declaration of struct cmbuf


    'C:  long outbuf[192];
    Dim outbuf(192) As Long
    'C: int ret;
    Dim ret As Integer
    'C: inbuf[0] = onoff;
    inbuf(0) = onoff
    'C: ret = DeviceIoControl(ATKACPIhandle, 0x222410, inbuf, sizeof(inbuf),
    'C: outbuf, sizeof(outbuf), &bytes, NULL);
    ret = DeviceIoControl(ATKACPIhandle, &H222410, inbuf, UBound(inbuf), _
    outbuf, UBound(outbuf), bytes, Null)
    'C: return ret;
    CtrlACPI = ret
End Function

'C: main() {
Sub Main()
    'C:  FILETIME stamp = {0};
    Dim stamp As FILETIME: stamp = 0
    'C:  long old = 0, count;
    Dim old As Long: old = 0
    Dim count As Long
    'C: ATKACPIhandle = CreateFile("\\\\.\\HOTKEY", GENERIC_READ|GENERIC_WRITE,
    'C:  FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
    'C:  NULL);
    Dim ATKACPIhandle As Long
    ATKACPIhandle = CreateFile("\\\\.\\HOTKEY", GENERIC_READ Or GENERIC_WRITE, _
        FILE_SHARE_READ Or FILE_SHARE_WRITE, Null, OPEN_EXISTING, 0, _
        Null)
    'C: if (!ATKACPIhandle) {
    If (ATKACPIhandle = 0) Then
        'C: MessageBox(NULL,"Fout bij laden vor driver","ERROR",MB_ICONERROR);
        MsgBox "Error loading driver", vbCritical, "ERROR"
        'C: exit(0);
    'C: }
    'C: CtrlACPI(1);
    Call CtrlACPI(1)
End Sub