Solved

Passing String To DLL Function - String Not Returned Properly

Posted on 2010-09-22
6
387 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
[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
  • 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 500 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
Industry Leaders: 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!

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

689 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