Solved

PChar equivalent in VB

Posted on 2004-10-17
6
1,671 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

10 Experts available now in Live!

Get 1:1 Help Now