Solved

C to VB 5 porting

Posted on 1998-12-22
7
186 Views
Last Modified: 2010-05-03
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
0
Comment
Question by:mbaker123
  • 3
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
prozak earned 100 total points
ID: 1451938
Try this:

'Declare the DLL function
Declare Function SAFEInitGlobal lib "dllname.dll" (ByVal strTheString As String)

Dim GlobalBlock as String

'Initialize your string (otherwise you'll get a protection fault when you call the DLL)
GlobalBlock = Space(5000)

'Call the DLL function
Call SAFEInitGlobal(GlobalBlock)

That should allow you to do it. Although the SAFEInitGlobal must be compiled stdcall (this refers to how arguments are passed to the function). Otherwise you'll get the error "Bad DLL calling convention" when you try to call SAFEInitGlobal.

Hope this helps...
0
 
LVL 1

Expert Comment

by:muffinthedog
ID: 1451939
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))

0
 
LVL 1

Expert Comment

by:prozak
ID: 1451940
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).
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:muffinthedog
ID: 1451941
Worked for me... passed a pointer to an array of bytes, kinda like a pointer to an array of characters...
0
 
LVL 1

Expert Comment

by:prozak
ID: 1451942
I stand corrected :)
0
 

Author Comment

by:mbaker123
ID: 1451943
Cheers I'll give that a try
Festive stuff
Matt
0
 
LVL 1

Expert Comment

by:muffinthedog
ID: 1451944
mbaker123...
    just noticed I wrote Dim ret as long.....
    should be Integer... sorry, just playing...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question