Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calling c++ dll's from visual basic

Posted on 2003-12-04
6
Medium Priority
?
772 Views
Last Modified: 2013-11-25
Hi,

I was given a dll function,

extern "C" {
DWORD MAGOCRTG_API InsertOCRTag( char   *lpTiffFileName,
                                                                 char   *lpTextFileName,
                                                                 DWORD  dwPagePointer);
}

The lpTextFileName is a string, lpTiffFileName is also a string and dwPagePointer should always be zero

I am supposed to be calling the above function from my vb application.

This is what I did.  Created a new vb project, added a module, and this line of code

Public declare function Merge Lib "C:\Program Files\GaussInterprise\DocView\MagOCRTg.dll" Alias "InsertOCRTag" (ByRef lpTiffFileName as String, ByRef lpTextFileName as String,ByRef dwpagepointer as integer") As Integer.

and inside a new form i m calling the function MERGE(X,Y,Z)

I know the part I am messing up is the datatypes.....Can anyone tell me how I can change my api call??   And wats DWORD, its not a structure , is it a "C" datatype ? Or is it something else I need to do apart from this?

Any help will be greatly appreciated

Thanks

-Paulin
0
Comment
Question by:shahpaulin
6 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 9876131
DWORD is a double word, you can just use a Long in VB.  Declare it as ByVal instead of ByRef (its not a pointer).

Regarding strings, thats not the recommended way of passing strings from VB to a C dll, but it sounds like you are stuck with it.  I dont think you can just pass a VB string into a char * data type.  However, you should be able to pass a byte array that contains the ascii values of the characters of the string. This question always seems to come up, and I always forget what is finally implemented regarding the char *.   haha
0
 

Author Comment

by:shahpaulin
ID: 9877881
Hi Azra,

Thanks for your reply.  
Didn't changed nething, jst changed the integer to long.  And it worked.
Seems char * or string isn't a problem or atleast for now =), coz the
function is doing wat it is supposed to.

-Paulin
0
 
LVL 5

Expert Comment

by:g0rath
ID: 9882112
Confir with this website

http://www.flipcode.com/articles/article_vbdlls.shtml

Strings aren't really that difficult after you understand that Microsft hides the details.

You should be using BSTR which is compatible with visual basic, when you typecast to a char *, the object will hide the details and give you a string to use inside your program.

But to pass a string back to Visual basic you cannot send a char *, you have to allocate new memory and return a BSTR.

BSTR __declspec(dllexport) CALLBACK StringExample(BSTR stringVar)
{
      LPSTR buffer;
      buffer = (LPSTR)stringVar;
      ::MessageBox(NULL,buffer,"in C++",0);
      buffer = _strrev(buffer);
      return(SysAllocString(stringVar));
}


This is from the example website above....I wrote a wrapper around my existing C/C++ functions specifically for a VB interface, so that my functions do not change.

by the way the LPSTR is Long Pointer to a String or rather a char *
the BSTR is actually a wchar_t * since it allows for I18N for windows...there are a series of functions that you can use to move them around from wide to narrow strings. For more info checkout the MSDN site.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:shahpaulin
ID: 9898530
Well I don't know what to do?  I found a solution by my self.
Azra's answer kinda helped......
ne suggestions?

can i award half the points to azra only?
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 1000 total points
ID: 9898696
Post a question in Community Support with your request.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 10267512
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered  AzraSound
 
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

leonstryker
EE Cleanup Volunteer
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

886 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