Solved

C to VB 5 porting

Posted on 1998-12-22
7
180 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now