?
Solved

PChar equivalent in VB

Posted on 2004-10-17
6
Medium Priority
?
1,791 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
[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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

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!

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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