Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calling c++ dll's from visual basic

Posted on 2003-12-04
6
Medium Priority
?
771 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses

688 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