none
createfile and deviceiocontrol function in vb.net RRS feed

  • Question

  • Hi guys,

    i wrote a driver for accessing gpio pins and it works perfect with my embedded visual c++ Application.


    #include "windows.h"
    //
    // Program entry point
    //
    #define IO_CTL_LED_1_ON 0x01
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPWSTR lpCmdLine, int nCmdShow) {
        
    HANDLE leddriver=CreateFile(L"LED1:",GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,0,NULL );
        

    if(!leddriver)
    {    

        MessageBox (NULL, TEXT ("ERROR OPENING LED!"), TEXT ("Hello2"), MB_OK);
    }



    DeviceIoControl(leddriver,IO_CTL_LED_1_ON,NULL,0,NULL,0,NULL,NULL);
    return 0;
    }

    I want to do the same application above in vb.net but i don't know what files should be included.

    I tried searching but i can't find any useful information.


    Thanks, Selva
    Monday, November 1, 2010 6:04 AM

Answers

  • You can pinvoke all the win32 methods into ..netcf.Check out the following article.
     
     
    Check out this site for declaration of CreateFile in vb.net
    Monday, November 1, 2010 7:14 AM
  • Also check this OpenNetCF article

    http://www.opennetcf.com/Default.aspx?tabid=127


    Vinoth.R http://vinoth-vinothblog.blogspot.com http://www.e-consystems.com
    Monday, November 1, 2010 7:18 AM
  • Hi,

    after some digging I found some old code samples I used before we switched to C#.

    The PInvoke Signatures are slightly different than yours.

     

    Imports System.IO
    Imports System.Windows.Forms.Form
    
    Public Class Form1
      Private handle1 As IntPtr
      Private Const genericRead As Integer = &H80000000
      Private Const genericWrite As Integer = &H40000000
      Private Const fileShareRead As Integer = 1
      Private Const fileShareWrite As Integer = 2
      Private Const openExisting As Integer = 3
      Private Const fileAttributeNormal As Integer = &H80
    
      Private Declare Function CloseHandle Lib "coredll.dll" (ByVal hObject As IntPtr) As Boolean
    
      Declare Function CreateFile Lib "coredll" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFLagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr
    
      Declare Function DeviceIoControl Lib "coredll" (ByVal hDevice As IntPtr, ByVal dwIoControlCode As Integer, ByVal lpInBuffer As IntPtr, ByVal nInBufferSize As Integer, ByVal lpOutBuffer As IntPtr, ByVal nOutBufferSize As Integer, ByRef lpBytesReturned As Integer, ByVal lpOverlapped As IntPtr) As Integer
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim IO_CTL_LED_1_ON As Integer
        Dim nBytesReturned As Integer
        IO_CTL_LED_1_ON = 1
    
        handle1 = CreateFile("LED1:", genericRead Or genericWrite, 0, IntPtr.Zero, openExisting, 0, IntPtr.Zero)
        If handle1 = -1 Then
          MsgBox("Open FAIL")
        Else
          Try
            DeviceIoControl(handle1, IO_CTL_LED_1_ON, IntPtr.Zero, 0, IntPtr.Zero, 0, nBytesReturned, IntPtr.Zero)
    
          Catch ex As Exception
            MsgBox("DeviceIo Fail")
          End Try
          CloseHandle(handle1)
        End If
      End Sub
    End Class
    

    HTH,

    Bjoern

    Wednesday, November 3, 2010 12:02 PM

All replies

  • You can pinvoke all the win32 methods into ..netcf.Check out the following article.
     
     
    Check out this site for declaration of CreateFile in vb.net
    Monday, November 1, 2010 7:14 AM
  • Also check this OpenNetCF article

    http://www.opennetcf.com/Default.aspx?tabid=127


    Vinoth.R http://vinoth-vinothblog.blogspot.com http://www.e-consystems.com
    Monday, November 1, 2010 7:18 AM
  • Hi Prabhu and Mohan,

     

    As iam a newbie for vb.net the things are not clear ,well enough to declare a function for Createfle and Deviceiocontrol.

    Also in  the link http://www.opennetcf.com/Default.aspx?tabid=127

    sample code link was dead.

     

     

    I have understood the following things clearly from the link

     

    When using P/Invoke, you must name the library (DLL) from which you are calling your functions. Unfortunately the functions that are on both the desktop and Windows CE are rarely in the same library. In the case of the Comm APIs, they're in kernel32.dll on the desktop and coredll.dll on CE. In addition, the desktop uses ANSI by default for its strings, but CE always uses Unicode. Both of these must be taken into account for the code to be portable.

     

    i also tried to add reference of a dll in vs2008--->coredll.dll from my Mips BSP-----------but it fails.

    Can you explain more ,if you have some timeor are you having any sample application with you .Please share with us.

     

    Thankyou


    Thanks, Selva
    Tuesday, November 2, 2010 5:49 AM
  • Hi prabhu and vinith,

    I have added a declaration for create file is it correct?NOW IT IS NOT SHOWING ANY ERRORS

     

    Imports System.IO
    Imports System.Windows.Forms.Form







    Public Class Form1

        Private Declare Function CloseHandle Lib "coredll.dll" (ByVal hObject As IntPtr) As Boolean
       

        Private Declare Function CreateFile Lib "coredll.dll" _
         (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
          ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
          ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
          ByVal hTemplateFile As IntPtr) As IntPtr


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        End Sub
    End Class


    Thanks, Selva
    Tuesday, November 2, 2010 6:08 AM
  • Tuesday, November 2, 2010 6:46 AM
  • Hi Prabhu i tried to do the following with the createfile but it returns -1 which is invalid any suggestions please.

     

     

     

    Imports System.IO
    Imports System.Windows.Forms.Form







    Public Class Form1



        Private handle1 As IntPtr
        Private portName As String = "LED1"
        Private Const GENERIC_READ As Int32 = &H80000000, GENERIC_WRITE As Int32 = &H40000000
        Private Const FILE_SHARE_READ As Int32 = 1, FILE_SHARE_WRITE As Int32 = 2
        Private Const OPEN_EXISTING As Int32 = 3, FILE_FLAG_WRITE_THROUGH As Int32 = 1
        Private Declare Function CloseHandle Lib "coredll.dll" (ByVal hObject As IntPtr) As Boolean
        

        Private Declare Function CreateFile Lib "coredll.dll" _
         (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
          ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
          ByVal dwCreationDisposition As Int32, ByVal dwFlagsAndAttributes As Int32, _
          ByVal hTemplateFile As IntPtr) As IntPtr




        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            handle1 = 0
            handle1 = CreateFile(portName, GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
            If handle1 = -1 Then

                MsgBox("FAIL")

            Else
                MsgBox("PASS")

            End If
        End Sub
    End Class


    Thanks, Selva
    Tuesday, November 2, 2010 9:14 AM
  • Hi,

    I think that the ":" character is missing in portName variable.

    AFAIK should read

    Private portName as String ="LED1:"

     

    HTH,

    Bjoern

    Tuesday, November 2, 2010 9:34 AM
  • Hi,

     

    OOPS GREAT CATCH Bjoernff,

     

    Thanks, it is working now .i will check with Deviceiocontrol and ping you back.

     

    Thankyou very much.

     


    Thanks, Selva
    Tuesday, November 2, 2010 10:14 AM
  • Hi,

    Iam facing problem in deviceiocontrol function of vb.net but the code made in embedded visual c++ works fine.Please correct me ,if you find any mistakes.


    EMBEDDED VISUAL C++ CODE

    #include "windows.h"
    //
    // Program entry point
    //
    #define IO_CTL_LED_1_ON 0x01
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPWSTR lpCmdLine, int nCmdShow) {
       
    HANDLE leddriver=CreateFile(L"LED1:",GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,0,NULL );
       
        MessageBox (NULL, TEXT ("are you getting anything!"), TEXT ("Hello2"), MB_OK);

    if(!leddriver)
    {   

        MessageBox (NULL, TEXT ("ERROR OPENING LED!"), TEXT ("Hello2"), MB_OK);
    }



    DeviceIoControl(leddriver,IO_CTL_LED_1_ON,NULL,0,NULL,0,NULL,NULL);
    return 0;
    }

     

     

    VB.NET

     

     


    Imports System.IO
    Imports System.Windows.Forms.Form







    Public Class Form1


        Private handle1 As IntPtr
        Private Const genericRead As Integer = &H80000000
        Private Const genericWrite As Integer = &H40000000
        Private Const fileShareRead As Integer = 1
        Private Const fileShareWrite As Integer = 2
        Private Const openExisting As Integer = 3
        Private Const fileAttributeNormal As Integer = &H80

        Private Declare Function CloseHandle Lib "coredll.dll" (ByVal hObject As IntPtr) As Boolean
       
        Declare Function CreateFile Lib "coredll" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFLagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer

        Declare Function DeviceIoControl Lib "coredll" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, ByRef lpInBuffer As IntPtr, ByVal nInBufferSize As Long, ByRef lpOutBuffer As IntPtr, ByVal nOutBufferSize As Long, ByRef lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long


       




        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim handle2 As Long
            Dim IO_CTL_LED_1_ON As Integer



            IO_CTL_LED_1_ON = 1

            handle1 = CreateFile("LED1:", genericRead Or genericWrite, 0, Nothing, openExisting, 0, Nothing)
            If handle1 = -1 Then

                MsgBox("FAIL")

            Else
                MsgBox("PASS")

            End If
            DeviceIoControl(handle1, IO_CTL_LED_1_ON, Nothing, 0, Nothing, 0, Nothing, Nothing)


        End Sub
    End Class

     

     

     

     

     

     

     

     

     

    Probably the problem could be in IO_CTL_LED_1_ON,   parmater2 of deviceiocntrol because in embedded C++

    I made the code as define like this and used like below

     


    #define IO_CTL_LED_1_ON 0x01

    DeviceIoControl(leddriver,IO_CTL_LED_1_ON,NULL,0,NULL,0,NULL,NULL);

     

    But in Vb.net i made it like this

     

    Dim IO_CTL_LED_1_ON As Integer


    IO_CTL_LED_1_ON = 1

    DeviceIoControl(handle1, IO_CTL_LED_1_ON, Nothing, 0, Nothing, 0, Nothing, Nothing)

     

    is it creating a ERROR in Wince as"Not Supported exception"


    Thanks, Selva
    Tuesday, November 2, 2010 12:27 PM
  • Hi,

    I am not sure, since I am not using VB but in all PInvokes for DeviceIoControl I have used sofar in C# the Overlapped parameter in DeviceIoControl was declared as IntPtr.

    HTH,

    Bjoern

    Tuesday, November 2, 2010 1:02 PM
  • Hi,

     

    I tried it but it didn't resolve my error.Thank you very much for the suggestion


    Thanks, Selva
    Tuesday, November 2, 2010 1:31 PM
  • Hi,

    can anybody help me with this issue?


    Thanks, Selva
    Tuesday, November 2, 2010 2:16 PM
  • Hi,

    after some digging I found some old code samples I used before we switched to C#.

    The PInvoke Signatures are slightly different than yours.

     

    Imports System.IO
    Imports System.Windows.Forms.Form
    
    Public Class Form1
      Private handle1 As IntPtr
      Private Const genericRead As Integer = &H80000000
      Private Const genericWrite As Integer = &H40000000
      Private Const fileShareRead As Integer = 1
      Private Const fileShareWrite As Integer = 2
      Private Const openExisting As Integer = 3
      Private Const fileAttributeNormal As Integer = &H80
    
      Private Declare Function CloseHandle Lib "coredll.dll" (ByVal hObject As IntPtr) As Boolean
    
      Declare Function CreateFile Lib "coredll" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Integer, ByVal dwFLagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr
    
      Declare Function DeviceIoControl Lib "coredll" (ByVal hDevice As IntPtr, ByVal dwIoControlCode As Integer, ByVal lpInBuffer As IntPtr, ByVal nInBufferSize As Integer, ByVal lpOutBuffer As IntPtr, ByVal nOutBufferSize As Integer, ByRef lpBytesReturned As Integer, ByVal lpOverlapped As IntPtr) As Integer
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim IO_CTL_LED_1_ON As Integer
        Dim nBytesReturned As Integer
        IO_CTL_LED_1_ON = 1
    
        handle1 = CreateFile("LED1:", genericRead Or genericWrite, 0, IntPtr.Zero, openExisting, 0, IntPtr.Zero)
        If handle1 = -1 Then
          MsgBox("Open FAIL")
        Else
          Try
            DeviceIoControl(handle1, IO_CTL_LED_1_ON, IntPtr.Zero, 0, IntPtr.Zero, 0, nBytesReturned, IntPtr.Zero)
    
          Catch ex As Exception
            MsgBox("DeviceIo Fail")
          End Try
          CloseHandle(handle1)
        End If
      End Sub
    End Class
    

    HTH,

    Bjoern

    Wednesday, November 3, 2010 12:02 PM
  • Hi,

     

    Thanks for your help Bjoern  There was a problem with my driver i made it success .Please watch the video here.

    http://www.youtube.com/watch?v=TqG724dBuE4


    Thanks, Selva
    Wednesday, November 3, 2010 12:50 PM