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