Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C to VB 5 porting

Posted on 1998-12-22
7
Medium Priority
?
204 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 200 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
Technology Partners: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

618 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