Solved

PChar equivalent in VB

Posted on 2004-10-17
6
1,710 Views
Last Modified: 2012-06-27
Hi

I am communicating with a Delphi DLL and I reference it by this

Public Declare Ansi Function GetERPStatus Lib "C:\Program Files\EasyCheck\EasyCheckLib.dll" Alias "GetERPStatus" (ByVal RFID As String, ByVal NLISID As String, ByVal PIC As String, ByVal StateCode As String, ByVal ProcType As String, ByRef Status As Byte, ByVal MaxLength As Integer) As Boolean

The "Status" variable is passed by reference so that the DLL can write to the variable.

If I do it as above then it will work but returns a number "79" dont think its that important, then funny things start happening, such as an exception in a different part of my code that isnt even called at any point.

I suspect It wirtes to this byte, but doesnt have enough space and writes over other things in memory

Here is the documentation that came with the DLL

GetERPStatus
Parameters
const lpRFID,
lpNLISID,
lpPIC,
lpStateCode,
lpProcType: PChar;
lpStatus: PChar;
dwMaxLength: Integer

Location
 - EASYCHECKLIB.DLL

If successful the lpStatus parameter will contain a string on the exception. These will be comma ‘,’ delimited for each exception and within the exception the fields are separated by the tilde ‘~’ character, as follows;

If i use other data types in the status variable then it just crashes

I have used the object data type and it returns this error
Additional information: Specified OLE variant is invalid.

If i use a string data type it just returns
Additional information: Object reference not set to an instance of an object.

Thanks
Adam
0
Comment
Question by:adam_pedley
  • 5
6 Comments
 
LVL 4

Author Comment

by:adam_pedley
ID: 12336106
Another thing

It works if I pass a byte object

It fails if I pass a byte array object
0
 
LVL 4

Author Comment

by:adam_pedley
ID: 12336636
Ok I can get the data type as a char and it returns "O" which is a good sign

So then i tried passing a char array but this error came up

#6: Ansi char arrays can not be marshaled as byref or as an unmanaged-to-managed parameter

char array as a byval doesnt work
0
 
LVL 4

Author Comment

by:adam_pedley
ID: 12336850
Ok i have actually found the problem, but dont know how to fix it

Its the way a Delphi/c++ DLL passes the string

It passes it as ANSI, where as vb uses unicode

Does anyone know how to solve this?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Author Comment

by:adam_pedley
ID: 12337008
Well after most of the day passed I found the answer

Public Declare Ansi Function GetERPStatus Lib "C:\Program Files\EasyCheck\EasyCheckLib.dll" Alias "GetERPStatus" (ByVal RFID As String, ByVal NLISID As String, ByVal PIC As String, ByVal StateCode As String, ByVal ProcType As String, ByVal Status As IntPtr, ByVal MaxLength As Integer) As Boolean
   
Dim tmpMemPath As IntPtr = Marshal.AllocHGlobal(255)
Dim result as boolean = GetERPStatus("", "", "3ALAF002", Convert.ToString(6), "D", tmpMemPath, 255)
Dim strValue As String = Marshal.PtrToStringAnsi(tmpMemPath)
MsgBox(strValue)

Basically I create a pointer first, with a length of 255, no data type defined
Then pass that point byval to the DLL
Then once the DLL has written to the pointer
I get an ANSI string from the pointer
And there it is

0
 
LVL 4

Author Comment

by:adam_pedley
ID: 12345616
If someone wants to copy and paste my solution, I will accept it as the answer

Just so this question can remain on EE

Thanks
Adam
0
 
LVL 2

Accepted Solution

by:
koruzica earned 500 total points
ID: 12610673
And this also :)

Well after most of the day passed I found the answer

Public Declare Ansi Function GetERPStatus Lib "C:\Program Files\EasyCheck\EasyCheckLib.dll" Alias "GetERPStatus" (ByVal RFID As String, ByVal NLISID As String, ByVal PIC As String, ByVal StateCode As String, ByVal ProcType As String, ByVal Status As IntPtr, ByVal MaxLength As Integer) As Boolean
   
Dim tmpMemPath As IntPtr = Marshal.AllocHGlobal(255)
Dim result as boolean = GetERPStatus("", "", "3ALAF002", Convert.ToString(6), "D", tmpMemPath, 255)
Dim strValue As String = Marshal.PtrToStringAnsi(tmpMemPath)
MsgBox(strValue)

Basically I create a pointer first, with a length of 255, no data type defined
Then pass that point byval to the DLL
Then once the DLL has written to the pointer
I get an ANSI string from the pointer
And there it is
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

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 …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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