Solved

PChar equivalent in VB

Posted on 2004-10-17
6
1,685 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

17 Experts available now in Live!

Get 1:1 Help Now