Forum: PC-Programmierung AtUsbHid.dll mit c# verwenden


von peter (Gast)


Lesenswert?

hat jemand schon mal versucht die dll von atmel für di kommunikation mit 
avrs einzubinden und damit was gemacht
weis dass das irgendwie mit p/invoke funktionieren muss aber hab 
vieleicht jemand ein kleines beispiel oder so danke

von peter (Gast)


Lesenswert?

bin jetzt ein bischen weiter nur bekomme ich jetzt ne fehlermeldung
vieleicht kann mir ja jemand weiterhelfen
wäre cool
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Runtime.InteropServices;
5
6
namespace ConsoleApplication1
7
{
8
    class Program
9
    {
10
        static void Main(string[] args)
11
        {
12
            Console.WriteLine("mein at 90 usb warper");
13
            at90usb.findHidDevice(0x046A, 0x0027);     // meine usb maus sollte ja acu erkannt werden
14
            Console.ReadKey();
15
            
16
        }
17
    }
18
    class at90usb
19
    {
20
        [DllImport("AtUsbHid.dll")]
21
        public static extern Boolean findHidDevice(uint VendorID, uint ProductID);
22
        [DllImport("AtUsbHid.dll")]
23
        public static extern void closeDevice();
24
25
    }
26
}

  

von Marcus (Gast)


Lesenswert?

Und warum teilst du die Fehlermeldung nicht mit?

Marcus

von Christian R. (mrrotzi)


Lesenswert?

ja, wo ist das Problem?

von peter (Gast)


Lesenswert?

ach ja sorry AccessViolationExeption

von peter (Gast)


Lesenswert?

mit ner anderne vid pid bekomm ich schön ein false zurück

von peter (Gast)


Lesenswert?

vieleicht passst es ja ach weil in der orginal anwendung von atmel gibt 
acu einen programmabsturz

von peter (Gast)


Lesenswert?

hier mal die header datei
1
//  Closes the USB device and all handles before exiting the application.
2
ATUSBHID_API void    STDCALL closeDevice(void);
3
4
ATUSBHID_API BOOLEAN STDCALL writeData(UCHAR* buf);
5
6
ATUSBHID_API BOOLEAN STDCALL readData(UCHAR* buffer);
7
8
ATUSBHID_API int     STDCALL hidRegisterDeviceNotification(HWND hWnd);
9
10
ATUSBHID_API void    STDCALL hidUnregisterDeviceNotification(HWND hWnd);
11
12
ATUSBHID_API int     STDCALL isMyDeviceNotification(DWORD dwData);
13
14
ATUSBHID_API BOOLEAN STDCALL setFeature(UCHAR type,UCHAR direction, unsigned int length);

wo ich noch nicht weis wie ichs machen soll ist das mit dem 
writeData(UCHAR* buf);
wie ich meinem char arrey oder string einen pointer zuweise
daentypen dies nicht gibt müsten prinzipiell mit unmanaget type gehen
aber wie geht das hab leider noch nichts gefunden

von peter (Gast)


Lesenswert?

HWND hWnd
ist mir auch nicht kalr
danke für die mühe

von Marcus (Gast)


Lesenswert?

Schau doch mal in den Weiten des WWW, da werden Sie geholfen.

Marcus

von peter (Gast)


Lesenswert?

1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Runtime.InteropServices;
5
6
namespace ConsoleApplication1
7
{
8
    class Program
9
    {
10
        static void Main(string[] args)
11
        {
12
            
13
14
            
15
            Console.WriteLine("mein at 90 usb warper");
16
            at90usb.closeDevice();
17
            Console.WriteLine(at90usb.findHidDevice(0x046B, 0x0027));
18
            Console.ReadKey();
19
            
20
        }
21
    }
22
    class at90usb
23
    {
24
        [DllImport("AtUsbHid.dll")]
25
        public static extern Boolean findHidDevice(uint VendorID, uint ProductID);
26
        [DllImport("AtUsbHid.dll")]
27
        public static extern void closeDevice();
28
        [DllImport("AtUsbHid.dll")]
29
        public static extern Boolean writeData(UCHAR* buf);
30
        [DllImport("AtUsbHid.dll")]
31
        public static extern Boolean readData(UCHAR* buffer);
32
        [DllImport("AtUsbHid.dll")]
33
        public static extern int hidRegisterDeviceNotification(int hWnd);
34
        [DllImport("AtUsbHid.dll")]
35
        public static extern void hidUnregisterDeviceNotification(int hWnd);
36
        [DllImport("AtUsbHid.dll")]
37
        public static extern int isMyDeviceNotification(UInt32 dwData);
38
        [DllImport("AtUsbHid.dll")]
39
        public static extern Boolean setFeature(byte type,byte direction, UInt32 length);
40
    }
41
}


jetzt hab ich halt noch das pointer problem
wie ich das genau übergeben kann
pointer ist ja eine ein zeiger auf eine speicheradresse was ja 
anumführsich nicht in c# unterstützt wird aber es sollte doch irgendwie 
gehen oder?

von Karl H. (kbuchegg)


Lesenswert?

> jetzt hab ich halt noch das pointer problem
> wie ich das genau übergeben kann
> pointer ist ja eine ein zeiger auf eine speicheradresse was ja
> anumführsich nicht in c# unterstützt wird aber es sollte doch irgendwie
> gehen oder?

http://www.google.com
"p/invoke string"

ist immer noch dein Freund.
Es gibt auch von Microsoft tonnenweise Doku darüber wie
man sowas macht.


von peter (Gast)


Lesenswert?

hab jetzt mal alles was ich so habe in in eine c# dll gepackt
vieleicht weis ja jemand weiter oder hat gar ein at90usbkey zum 
herumspielen
vieleicht meldet sich ja auch mal jemand mit konstruktifen hinweisen
und kann mir sagen was man noch anders machen muss
thx

meine c# dll
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Runtime.InteropServices;
5
6
namespace sharpAt90UsbLib
7
{
8
9
    public class At90Usb
10
    {
11
        private uint VendorID, ProductID;
12
        public uint Vendor_ID
13
        {
14
            get
15
            {
16
                return VendorID;
17
            }
18
            set
19
            {
20
                VendorID = value;
21
            }
22
        }
23
        public uint Product_ID
24
        {
25
            get
26
            {
27
                return ProductID;
28
            }
29
            set
30
            {
31
                ProductID = value;
32
            }
33
        }
34
35
        [DllImport("AtUsbHid.dll")]
36
        private static extern Boolean findHidDevice(uint VendorID, uint ProductID);
37
        public Boolean find_Hid_Device()
38
        {
39
            return findHidDevice(VendorID, ProductID);
40
           
41
        }
42
43
44
        [DllImport("AtUsbHid.dll")]
45
        private static extern void closeDevice();
46
        public void close_Device()
47
        {
48
            closeDevice();
49
        }
50
51
        [DllImport("AtUsbHid.dll")]
52
        public static extern Boolean writeData(byte[] buf);
53
54
        [DllImport("AtUsbHid.dll")]
55
        private static extern Boolean readData(ref byte buffer);
56
57
        [DllImport("AtUsbHid.dll")]
58
        private static extern int hidRegisterDeviceNotification(IntPtr hWnd);
59
60
        [DllImport("AtUsbHid.dll")]
61
        private static extern void hidUnregisterDeviceNotification(IntPtr hWnd);
62
63
        [DllImport("AtUsbHid.dll")]
64
        private static extern int isMyDeviceNotification(uint dwData);
65
66
        [DllImport("AtUsbHid.dll")]
67
        private static extern Boolean setFeature(byte type, byte direction, uint length);
68
    }
69
}


c++ header datei von atmel
1
 
2
#ifndef _ATUSBHID_H_
3
#define _ATUSBHID_H_
4
5
// Error codes.
6
#define ERROR_USB_DEVICE_NOT_FOUND          0xE0000001
7
#define ERROR_USB_DEVICE_NO_CAPABILITIES    0xE0000002
8
9
// name of the DLL to be loaded
10
#define AT_USB_HID_DLL "AtUsbHid"
11
12
// Implement the DLL export/import mechanism and allow a C-written program
13
// to use our DLL.
14
#ifdef ATUSBHID_EXPORTS
15
#define ATUSBHID_API extern "C" __declspec(dllexport)
16
#else
17
#define ATUSBHID_API extern "C" __declspec(dllimport)
18
#endif
19
20
21
// This macro function calls the C runtime's _beginthreadex function. 
22
// The C runtime library doesn't want to have any reliance on Windows' data 
23
// types such as HANDLE. This means that a Windows programmer needs to cast
24
// values when using _beginthreadex. Since this is terribly inconvenient, 
25
// this macro has been created to perform the casting.
26
typedef unsigned(__stdcall *PTHREAD_START)(void *);
27
28
#define chBEGINTHREADEX(psa, cbStack, pfnStartAddr, \
29
   pvParam, fdwCreate, pdwThreadId)                 \
30
      ((HANDLE)_beginthreadex(                      \
31
         (void *)        (psa),                     \
32
         (unsigned)      (cbStack),                 \
33
         (PTHREAD_START) (pfnStartAddr),            \
34
         (void *)        (pvParam),                 \
35
         (unsigned)      (fdwCreate),               \
36
(unsigned *)(pdwThreadId)))
37
38
// Allow applications not built with Microsoft Visual C++ to link with our DLL.
39
#define STDCALL __stdcall
40
41
// These macros make calling our DLL functions through pointers easier.
42
#define DECLARE_FUNCTION_POINTER(FUNC)  PF_##FUNC lp##FUNC=NULL;  
43
#define LOAD_FUNCTION_POINTER(DLL,FUNC) lp##FUNC = (PF_##FUNC)GetProcAddress(DLL, #FUNC);
44
#define ADDR_CHECK(FUNC) if (lp##FUNC == NULL) {fprintf(stderr,"%s\n", "Error: Cannot get address of function."); return FALSE;}
45
#define DYNCALL(FUNC) lp##FUNC
46
47
48
///////////////////////////////////////////////////////////////////////////////
49
typedef BOOLEAN (STDCALL *PF_findHidDevice)(const UINT VendorID, const UINT ProductID);
50
typedef void    (STDCALL *PF_closeDevice)(void);
51
typedef BOOLEAN (STDCALL *PF_writeData)(UCHAR* buffer);
52
typedef BOOLEAN (STDCALL *PF_readData)(UCHAR* buffer);
53
typedef int     (STDCALL *PF_hidRegisterDeviceNotification)(HWND hWnd);
54
typedef void    (STDCALL *PF_hidUnregisterDeviceNotification)(HWND hWnd);
55
typedef int     (STDCALL *PF_isMyDeviceNotification)(DWORD dwData);
56
typedef BOOLEAN (STDCALL *PF_startBootLoader)(void);
57
typedef BOOLEAN (STDCALL *PF_setFeature)(UCHAR type,UCHAR direction, unsigned int length);
58
///////////////////////////////////////////////////////////////////////////////
59
60
// Exported functions prototypes.
61
62
///////////////////////////////////////////////////////////////////////////////
63
ATUSBHID_API BOOLEAN STDCALL findHidDevice(const UINT VendorID, const UINT ProductID);
64
65
//  Closes the USB device and all handles before exiting the application.
66
ATUSBHID_API void    STDCALL closeDevice(void);
67
68
ATUSBHID_API BOOLEAN STDCALL writeData(UCHAR* buf);
69
70
ATUSBHID_API BOOLEAN STDCALL readData(UCHAR* buffer);
71
72
ATUSBHID_API int     STDCALL hidRegisterDeviceNotification(HWND hWnd);
73
74
ATUSBHID_API void    STDCALL hidUnregisterDeviceNotification(HWND hWnd);
75
76
ATUSBHID_API int     STDCALL isMyDeviceNotification(DWORD dwData);
77
78
ATUSBHID_API BOOLEAN STDCALL setFeature(UCHAR type,UCHAR direction, unsigned int length);
79
80
///////////////////////////////////////////////////////////////////////////////
81
82
#ifndef ATUSBHID_EXPORTS
83
84
85
DECLARE_FUNCTION_POINTER(findHidDevice);
86
DECLARE_FUNCTION_POINTER(closeDevice);
87
DECLARE_FUNCTION_POINTER(writeData);
88
DECLARE_FUNCTION_POINTER(readData);
89
DECLARE_FUNCTION_POINTER(hidRegisterDeviceNotification);
90
DECLARE_FUNCTION_POINTER(hidUnregisterDeviceNotification);
91
DECLARE_FUNCTION_POINTER(isMyDeviceNotification);
92
DECLARE_FUNCTION_POINTER(setFeature);
93
94
// this function call all function available in the DLL *
95
static bool loadFuncPointers(HINSTANCE hLib)
96
{
97
    LOAD_FUNCTION_POINTER(hLib, findHidDevice);
98
    ADDR_CHECK(findHidDevice);
99
100
  LOAD_FUNCTION_POINTER(hLib, closeDevice);
101
    ADDR_CHECK(closeDevice);
102
103
  LOAD_FUNCTION_POINTER(hLib, writeData);
104
    ADDR_CHECK(writeData);
105
106
  LOAD_FUNCTION_POINTER(hLib, readData);
107
    ADDR_CHECK(readData);
108
109
  LOAD_FUNCTION_POINTER(hLib, hidRegisterDeviceNotification);
110
  ADDR_CHECK(hidRegisterDeviceNotification);
111
112
  LOAD_FUNCTION_POINTER(hLib, hidUnregisterDeviceNotification);
113
  ADDR_CHECK(hidUnregisterDeviceNotification);
114
115
  LOAD_FUNCTION_POINTER(hLib, isMyDeviceNotification);
116
  ADDR_CHECK(isMyDeviceNotification);
117
118
  LOAD_FUNCTION_POINTER(hLib, setFeature);
119
  ADDR_CHECK(setFeature);
120
121
    return true;
122
}
123
124
#endif
125
126
#endif  // _ATUSBHID_H_


von peter (Gast)


Lesenswert?

bin ja immer offen für neue ideen und wollte nur mal wissen ob das was 
ich da vor habe in die richtige richtung geht
und c# ist halt doch etwas feiner für gui anwendungen
vieleicht hat ja jemand lut mitzumachen oder so

von peter (Gast)


Lesenswert?

http://www.codeproject.com/useritems/USB_HID.asp


hab jetzt sogar schon was fertiges gefunden hoffe ich bekomms zu laufen 
werd ich gleich mat testen

von Marcus (Gast)


Lesenswert?

Dann weisst du jetzt ja auch warum dein Programm beim Suchen deiner Maus 
nen Fehler wirft.

Marcus

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.