[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Passing String To DLL Function - String Not Returned Properly

Posted on 2010-09-22
6
Medium Priority
?
390 Views
Last Modified: 2012-08-14
I'm trying to call a DLL function, passing it a string, expecting it to modify the string and return it.

Function Prototype:

Declare Function GetLastError Lib "DBAPI2.dll" (ByRef msg As StringBuilder) As Integer

String Declaration:

 <MarshalAs(UnmanagedType.LPStr)> Public tmsg As New StringBuilder("Default" & Chr(0), 4096)
According to documentation, the function should expect to be passed a string of size 4096.

Current Usage:

Result = GetLastError(tmsg)

Calling GetLastError should return an error message placed in tmsg. Whenever I call GetLastError though, I'm either returned an empty string, or 1 weird character.

Am I doing something wrong in my implementation here? Any insight?
0
Comment
Question by:diablo089
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33739485
Can you post the documentation and examlpe in the original language it was written?
You can marshal it <Out()> Byval


Declare Function GetLastError Lib "DBAPI2.dll" (Byval msg As StringBuilder) As Integer

Open in new window

0
 

Author Comment

by:diablo089
ID: 33739942
The API and libraries are Embarcadero (Borland) Dephi Win32.

Here's the original function prototype in Delphi:

function GetLastError(Msg: pChar): integer;
  stdcall; external 'DBAPI2.dll';

I was told by one of the guys that works with the API is that the function expects a string of size 4096.

Attached is some info I got from one of the developers that occasionally works with the API he wrote in C# that apparently works.



Prototype:

namespace TestDBAPI
{
                class DBAPI
                {
 [DllImport(dllname, CharSet = CharSet.Ansi)]
public static extern int GetLastError(StringBuilder msg);
                }
}

Usage:

StringBuilder msg = new StringBuilder(4096);
DBAPI.GetLastError(msg);

Open in new window

0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 2000 total points
ID: 33739972
Okay.. You can try something like this
<DllImport("DBAPI2.dll", CharSet:=CharSet.Ansi)> _
    Private Shared Function GetLastError(ByVal msg As System.Text.StringBuilder) As Integer
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim msg As New System.Text.StringBuilder(4096)
        GetLastError(msg)
    End Sub

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:diablo089
ID: 33740152
Okay, I tried what you suggested as is, and I can't get anything put into "msg". If I switch to ByRef msg, I either get one character or a string of characters, but it's either a random character or a mess of random characters. Nothing discernible as a word or phrase.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 33740185
Do you know if it always returns "something" regardless? For example would it say "no error" if you never called into ANY other exported routine that would write the error from that routine into the buffer.
Lets just says the DLL exported a function called Initalize() if initialize failed it would write the error string into the buffer but if you never called any other method it could just return NULL unless there is *always* a default string that is suppose to be returned if only calling GetLastError...
0
 

Author Comment

by:diablo089
ID: 33740255
Hmm... not sure if it always returns something. I'll see if can find out.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

834 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