CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Sep 2010

    C++ unmanaged code vs. managed VB.net


    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

    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
    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");
    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
    '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, _
    IntPtr.Zero, _
    If devHandle = usbio_i.INVALID_HANDLE_VALUE Then
    SetLastErrorString("Unable to open the USBIO device")
    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.


  2. #2
    Join Date
    Dec 2009

    Re: C++ unmanaged code vs. managed VB.net

    You have:
    sDevice = "\\?\usb#vid_0765&pid_" & device & "#******#{" & PRIVATE_IID_STR & "}"
    I'm curious about where and how you delcared "device".

  3. #3
    Join Date
    Dec 2009

    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.

  4. #4
    Join Date
    Sep 2010

    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.

  5. #5
    Join Date
    Dec 2009

    Re: C++ unmanaged code vs. managed VB.net

    Glad to help.

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.