Solved

Calling c++ dll's from visual basic

Posted on 2003-12-04
6
766 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
[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
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…
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…

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