?
Solved

PChar equivalent in VB

Posted on 2004-10-17
6
Medium Priority
?
1,845 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 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