Link to home
Start Free TrialLog in
Avatar of mbaker123
mbaker123

asked on

C to VB 5 porting

Help
I'm having trouble converting a 3rd party example program from C to VB (5 Pro), they don't support VB :+(

C code
unsigned char *GlobalBlock = (unsigned char *)new unsigned char[5000];

Where GlobalBlock is used in the initialisation function
SAFEInitGlobal(GlobalBlock);

and the function prototype is
extern "C" short __declspec(dllexport) WINAPI SAFEInitGlobal(unsigned char*);

My C is very rusty so any help will be much appreciated.

Well Have a good winter holiday one and all.

MATT
ASKER CERTIFIED SOLUTION
Avatar of prozak
prozak

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of muffinthedog
muffinthedog

mbaker123,
    should be able to call it like the following but by the name of the function, it seems it's just allocating global memory. Do you have the body of the function?

Declare Function SAFEInitGlobal Lib "Whatever" (lpChar As Any) As Integer


   Dim bte(5000) As Byte
   Dim ret As Long
   ret = SAFEInitGlobal(bte(0))

Actually I pretty sure that muffinthedog's comment will not work. VB uses OLE SAFEARRAYs to store data in an array. Unlike C the memory is managed by windows and when you pass an array to a C function, you're not passing the block of elements. Your passing a pointer to a SAFEARRAY structure. I can pretty much guarantee that it will crash if you do it that way (passing an array of bytes).
Worked for me... passed a pointer to an array of bytes, kinda like a pointer to an array of characters...
I stand corrected :)
Avatar of mbaker123

ASKER

Cheers I'll give that a try
Festive stuff
Matt
mbaker123...
    just noticed I wrote Dim ret as long.....
    should be Integer... sorry, just playing...