473,388 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

LPCSTR marshalling to unmanaged dll

I wish to call an unmanaged function from vb delacred as so:

#define DllExport __declspec(dllexport)
DllExport int WINAPI DllAlcWrite(HANDLE, LPCSTR, int);
I have tried:

1.)
Private Declare Ansi Function DllAlcWrite Lib "alcsapi" (ByVal hConn
As Long, ByVal strGWOutgoing As String, ByVal intStringLength As
Integer) As Integer

<VBFixedString(255)> Private strOutgoingBuffer As String
strOutgoingBuffer ="FOO"
intReturn = DllAlcWrite(m_Handle, strOutgoingBuffer, 3)

2.)
<DllImport("alcsapi.dll")> _
Private Shared Function DllAlcWrite(ByVal hConn As Long, ByVal
strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
End Function
<VBFixedString(255)> Private strOutgoingBuffer As String
strOutgoingBuffer ="FOO"
intReturn = DllAlcWrite(m_Handle, strOutgoingBuffer, 3)

3.)
<DllImport("alcsapi.dll", CharSet:=CharSet.Ansi,
CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function DllAlcWrite(ByVal hConn As Long, ByVal
strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
End Function
<VBFixedString(255)> Private strOutgoingBuffer As String
strOutgoingBuffer ="FOO"
intReturn = DllAlcWrite(m_Handle, strOutgoingBuffer, 3)

( i know the handle is long, but it is working for other functions in
this dll)

I have also tried setting the string length (3rd parm)to 255
I am able to call other functions in this dll ok
=================================

All of them generate the NullReference exception. Is there a way to
properly marshal LPCSTR?

Help!
Nov 20 '05 #1
6 2239
( i know the handle is long, but it is working for other functions in
this dll)


Did you mean to say that it isn't a Long? Because it isn't, and you
should replace it with IntPtr or Integer.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #2
Mattias,

It doesn't seem to matter, I still get a nullreference exception with
this:

<DllImport("alcsapi.dll", CharSet:=CharSet.Ansi,
CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function DllAlcWrite(ByVal hConn As Long, ByVal
strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
End Function
<DllImport("alcsapi.dll")> _
Private Shared Function DllInitAlc(ByVal Board As Integer, ByVal
lngLine As Integer, _
ByVal GWName As String, ByVal IA As Integer, ByVal TA As Integer) As
IntPtr
End Function

Dim m_handle as IntPtr
m_Handle=DllInitAlc(CInt(lngBoard), CInt(lngLine), strServer,
lngIAHex, lngTAHex)
'(got a non zero handle)
<VBFixedString(255)> Private strOutgoingBuffer As String
strOutgoingBuffer ="FOO"

intReturn = DllAlcWrite(m_Handle, strOutgoingBuffer, 3)
'(exception)

Mattias Sjögren <ma********************@mvps.org> wrote in message news:<eO**************@TK2MSFTNGP10.phx.gbl>...
( i know the handle is long, but it is working for other functions in
this dll)


Did you mean to say that it isn't a Long? Because it isn't, and you
should replace it with IntPtr or Integer.

Mattias

Nov 20 '05 #3
John,
<DllImport("alcsapi.dll", CharSet:=CharSet.Ansi,
CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function DllAlcWrite(ByVal hConn As Long, ByVal
strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
End Function


Did you change anything? hConn is still declared as Long.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #4
I changed it to intPtr and stilll have the same problem.
Mattias Sjögren <ma********************@mvps.org> wrote in message news:<eR*************@tk2msftngp13.phx.gbl>...
John,
<DllImport("alcsapi.dll", CharSet:=CharSet.Ansi,
CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function DllAlcWrite(ByVal hConn As Long, ByVal
strGWOutgoing As String, ByVal intStringLength As Integer) As Integer
End Function


Did you change anything? hConn is still declared as Long.

Mattias

Nov 20 '05 #5
On 19 May 2004 14:48:51 -0700, John Lucas wrote:
I wish to call an unmanaged function from vb delacred as so:

#define DllExport __declspec(dllexport)
DllExport int WINAPI DllAlcWrite(HANDLE, LPCSTR, int);
I have tried:

1.)
Private Declare Ansi Function DllAlcWrite Lib "alcsapi" (ByVal hConn
As Long, ByVal strGWOutgoing As String, ByVal intStringLength As
Integer) As Integer

Private Declare Ansi Function DllAlcWrite Lib "alcsapi.dll" _
(ByVal hConn As IntPtr, _
ByVal strGWOutgoing As String, _
ByVal intStringLength As Integer) As Integer

' air code :)
Dim buffer As String = new String(vbNullChar, 255)
DllAlcWrite (hConn, buffer, buffer.Length)

HTH
--
Tom Shelton [MVP]
Nov 20 '05 #6
Tom,

That was it! Null termination character.

Here's what I ended up with.

Dim buffer As String
buffer = strSendThis
buffer.PadRight(255, vbNullChar)
intReturn = DllAlcWrite(m_SabreHandle, buffer, buffer.Length)
THANK YOU.
-john

Tom Shelton <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message news:<12******************************@40tude.net> ...
On 19 May 2004 14:48:51 -0700, John Lucas wrote:
I wish to call an unmanaged function from vb delacred as so:

#define DllExport __declspec(dllexport)
DllExport int WINAPI DllAlcWrite(HANDLE, LPCSTR, int);
I have tried:

1.)
Private Declare Ansi Function DllAlcWrite Lib "alcsapi" (ByVal hConn
As Long, ByVal strGWOutgoing As String, ByVal intStringLength As
Integer) As Integer

Private Declare Ansi Function DllAlcWrite Lib "alcsapi.dll" _
(ByVal hConn As IntPtr, _
ByVal strGWOutgoing As String, _
ByVal intStringLength As Integer) As Integer

' air code :)
Dim buffer As String = new String(vbNullChar, 255)
DllAlcWrite (hConn, buffer, buffer.Length)

HTH

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PHil Coveney | last post by:
Hello, I am having difficulty marshalling structures when calling DeviceIoControl. I am importing this Win32 function as static extern int DeviceIoControl (int hDevice, int...
1
by: MuZZy | last post by:
Hello, I am probably facing a sort of language barrier, but i can't really get what 'marshalling' is in terms of .NET Is it a kind of type casting? And when do you use it? Thank you, Andrey
4
by: TT (Tom Tempelaere) | last post by:
Hi people I am wrapping a C dll using PInvoke from C#. I need to wrap the following signature in C int dma_start( const UCHAR* data, UINT data_length ) The function should start with a DMA...
2
by: Rookie | last post by:
Hi, I had a question on DllImport. On importing a function from a VC++ dll using DllImport (to a C# program), the function argument data types and the return types may be of a type that is not...
3
by: George Ter-Saakov | last post by:
How do i call from managed C++ function wich accepts LPCSTR? void AddName( System::String *sName ) { AddName( (LPCSTR) sName )); -- does not compile. } George.
13
by: Joe Thompson | last post by:
Hi, I am using VS 2003 writing a managed C++ windows app. I am calling PlaySound. The first parameter is a LPCSTR. I have a String *str representing a filename. I like using the String class...
2
by: BartMan | last post by:
Greetings, When working with managed c++, do you have to do anything special when going from simple types from managed to unmanaged and vice versa. Or is marshalling handled automatically for...
3
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
3
by: Saad | last post by:
Hi, I have a struct as follows: public __gc struct STTemp { public: int someint; System::Collections::ArrayList* arrTemp;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.