-
September 19th, 2010, 10:34 AM
#1
C++ unmanaged code vs. managed VB.net
Hi,
I am trying to develop a dll in VisualStudio2005 in vb.net that will communicate with a spectrometer attached to the USB port of the computer.
Now the dll exists in C++ and it works like a charm. It is unmanaged code.
Now if I try to translate this code in VB.net (because our app is in VB and my boss wants it in VB for maintainability) I have problems.
First of all, I am trying to create a file to open that port. The code in C++ looks like below and it works. Needles to say I tried to call the C++ dll from our app and it works. If I call the vb dll it doesn't. It gives me "access denied" no matter what I tried. I am not sure if my CreateFile is wrong or it's .net. I read somewhere that I need a manifest file included in the dll? Has anyone experience something like this? Why would i get "ACCESS DENIED" all the time?
// close and clear current stuff
ClosePort();
swprintf( sDevice, L"\\\\?\\usb#vid_0765&pid_%s#******#{%s}", Device, PRIVATE_IID_STR );
/////////////////////////////////////////////////////////////////////
// open the device
DevHandle = CreateFile(
sDevice, // device name
GENERIC_READ | GENERIC_WRITE, // access mode
FILE_SHARE_WRITE | FILE_SHARE_READ, // share mode
NULL, // security descriptor
OPEN_EXISTING, // how to create
FILE_FLAG_OVERLAPPED, // use overlapped I/O for reading
NULL ); // template file
if ( DevHandle == INVALID_HANDLE_VALUE )
{
SetLastErrorString("Unable to open the USBIO device");
ClosePort();
return FALSE;
}
I am trying to do the same thing in VB and I am always getting Access_denied. I can’t figure out why? I am running under Vista. Here is the code
Declare Auto Function CreateFile Lib "kernel32" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Int32, _
ByVal dwShareMode As Int32, _
ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Int32, _
ByRef dwFlagsAndAttributes As Int32, _
ByVal hTemplateFile As IntPtr) As IntPtr
Dim sDevice As String
'close and clear current stuff
ClosePort()
'sprintf( sDevice, "\\\\?\\usb#vid_0765&pid_%s#******#{%s}", Device, PRIVATE_IID_STR );
sDevice = "\\?\usb#vid_0765&pid_" & device & "#******#{" & PRIVATE_IID_STR & "}"
'open the device
devHandle = CreateFile(sDevice, GENERIC_READ Or GENERIC_WRITE, _
FILE_SHARE_WRITE Or FILE_SHARE_READ, _
IntPtr.Zero, _
OPEN_EXISTING, _
FILE_FLAG_OVERLAPPED, _
IntPtr.Zero)
If devHandle = usbio_i.INVALID_HANDLE_VALUE Then
SetLastErrorString("Unable to open the USBIO device")
ClosePort()
Return False
End If
Where the constants are as it follows
Const GENERIC_READ As Integer = &H80000000
Const GENERIC_WRITE As Integer = &H40000000
Const FILE_SHARE_WRITE As Integer = &H2
Const FILE_SHARE_READ As Integer = &H1
Const FILE_FLAG_OVERLAPPED As Integer = 40000000
Const OPEN_EXISTING As Integer = 3
Do you have any idea why createFile always gives me Aceess denied? Any help is greatly appreciated.
Thank you so much.
Adria
-
September 19th, 2010, 10:45 AM
#2
Re: C++ unmanaged code vs. managed VB.net
You have:
Code:
sDevice = "\\?\usb#vid_0765&pid_" & device & "#******#{" & PRIVATE_IID_STR & "}"
I'm curious about where and how you delcared "device".
-
September 19th, 2010, 02:12 PM
#3
Re: C++ unmanaged code vs. managed VB.net
I just noticed that in the C++ version you NULL as the last argument and have IntPtr.zero in the VB version. IntPtr.zero does not equal null. You might want to try it with keyword "Nothing" instead.
-
September 20th, 2010, 10:13 AM
#4
Re: C++ unmanaged code vs. managed VB.net
Viperbyte thank you for your answer. device is passed in. It is an argument of the Connect function
Public Function OpenPort(ByRef device As String) As Boolean
....
Thank you for your observation. Replacing IntPtr with Nothing worked. I was able to open the port.
-
September 20th, 2010, 10:52 AM
#5
Re: C++ unmanaged code vs. managed VB.net
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|