Solved

Calling c++ dll's from visual basic

Posted on 2003-12-04
6
761 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

757 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

27 Experts available now in Live!

Get 1:1 Help Now