Solved

Passing String To DLL Function - String Not Returned Properly

Posted on 2010-09-22
6
382 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 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now