Solved

C to VB 5 porting

Posted on 1998-12-22
7
188 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
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

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!

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

756 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