Solved

Passing String To DLL Function - String Not Returned Properly

Posted on 2010-09-22
6
385 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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