Solved

Calling c++ dll's from visual basic

Posted on 2003-12-04
6
762 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Explain Unit of Work pattern 2 59
Copy a row 12 59
Notepad++ how to remove delimiter : from beggning of the line? 3 94
Add and format columns in vb6 7 47
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 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

21 Experts available now in Live!

Get 1:1 Help Now