?
Solved

Passing String To DLL Function - String Not Returned Properly

Posted on 2010-09-22
6
Medium Priority
?
388 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 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
Technology Partners: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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