Solved

C to VB 5 porting

Posted on 1998-12-22
7
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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