Seite 1 von 1

Wo Header mit QLibrary einbinden?

Verfasst: 21. August 2009 21:41
von lodo2609
Hallo zusammen,

ich habe mir eine Header-Datei gebastelt, um auf eine dll zugreifen zu können, hier ein Ausschnitt

Code: Alles auswählen

#ifndef MPUSBAPI_QT_H
#define MPUSBAPI_QT_H
#include <QLibrary>
#include "windows.h"

#define	MPUSB_FAIL                  0
#define MPUSB_SUCCESS               1

#define MP_WRITE                    0
#define MP_READ                     1


#define MAX_NUM_PIC_DEV           127

QLibrary myLib("mpusbapi.dll");
typedef DWORD (*MPUSBGetDLLVersion)(void);
typedef DWORD (*MPUSBGetDeviceCount)(char* pVID_PID);
typedef HANDLE (*MPUSBOpen)(DWORD instance,         // Input
                 char* pVID_PID,            // Input
                 char* pEP,                 // Input
                 DWORD dwDir,               // Input
                 DWORD dwReserved);
typedef DWORD (*MPUSBRead)(HANDLE handle,   // Input
                char* pData,                // Output
                DWORD dwLen,                // Input
                DWORD* pLength,             // Output
                DWORD dwMilliseconds);      // Input
typedef DWORD (*MPUSBWrite)(HANDLE handle,  // Input
                 PVOID pData,               // Input
                 DWORD dwLen,               // Input
                 DWORD* pLength,            // Output
                 DWORD dwMilliseconds);     // Input
typedef DWORD (*MPUSBReadInt)(HANDLE handle,// Input
                   void* pData,             // Output
                   DWORD dwLen,             // Input
                   DWORD* pLength,          // Output
                   DWORD dwMilliseconds);   // Input
typedef bool (*MPUSBClose)(HANDLE handle);

MPUSBGetDLLVersion PICGetDLLVersion = (MPUSBGetDLLVersion) myLib.resolve("_MPUSBGetDLLVersion");
MPUSBGetDeviceCount PICGetDeviceCount = (MPUSBGetDeviceCount) myLib.resolve("_MPUSBGetDeviceCount");
MPUSBOpen PICOpen = (MPUSBOpen) myLib.resolve("_MPUSBOpen");
MPUSBRead PICRead = (MPUSBRead) myLib.resolve("_MPUSBRead");
....

Wie muss ich nun vorgehen, wenn ich diese Funktionen (PICGetDLLVersion, etc.) in verschiedenen Units eines Projektes nutzen möchte? Wenn ich sie überall, wo ich sie brauche mit

Code: Alles auswählen

#include "mpusbapi_qt.h"
einbinde, erhalte ich die Fehlermeldung

error: multiple definition of `myLib'
error: multiple definition of `PICGetDLLVersion'

Wo muss die Datei im Projekt einbinden, damit ich von allen Units aus drauf zugreifen kann und der Fehler nicht auftaucht?

Vielen Dank und Gruss
Lodo2609

Verfasst: 21. August 2009 23:10
von Christian81
Was soll das denn?

Code: Alles auswählen

QLibrary myLib("mpusbapi.dll"); 
Und was hat das mit Qt zu tun außer der doch etwas komischen QLibrary - Verwendung? Wie man eine Shared Library und deren Header benutzt ist nicht wirklich Qt-spezifisch...

Verfasst: 22. August 2009 11:16
von lodo2609
Hallo,

wieso
etwas komischen QLibrary - Verwendung
?

Ich bin neu im Umgang mit Qt und habe nur die gefundenen Beispiele
http://www.qtforum.org/article/15942/us ... th-qt.html
zur Verwendung von QLibrary abgeändert? Wie müsste ich dass denn "richtig" machen?

Gruss
Lodo2609

Verfasst: 22. August 2009 11:27
von Christian81
Noch einmal - Um in Qt einen externe shared lib zu benutzen muss man an der shared-lib nichts ändern. Es ändert sich nichts ob Qt nun dabei ist oder nichts.

Verfasst: 22. August 2009 11:50
von franzf
Weil du versuchst QLibrary als Ersatz für ordentliches Linken zu verwenden. Das ist einfach Mist ;)
QLibrary braucht keinen Header, QLibrary extrahiert Symbole (Funktionen).

Und dass multiple definitions kommen ist auch klar, wenn du den Header einbindest. Du deklarierst ein globales Objekt myLib. Beim includieren in mehrere Units existiert das einfach auch mehrfach, und deshalb steigt der Linker aus. Wenn das Design wirklich der Weiheit letzter Schluss ist (was ich stark bezweifle...) suchst du das Schlüsselwort "extern". Lies dich darüber schlau.

Aber besser wäre, wenn du erklären würdest, was du vor hast, wie die Bedingungen sind, usw. Dann findet man auch eine gute Lösung.

Grüßle...

Verfasst: 22. August 2009 22:25
von lodo2609
Hallo,

ich habe eine dll-Datei (mpusbapi.dll) zur Kommunikation mit einem einem PIC Mikrocontroller über USB und eine Headerdatei

Code: Alles auswählen

/*********************************************************************
 *
 *                  MPUSBAPI Library
 *
 *********************************************************************
 * FileName:        mpusbapi.h
 * Dependencies:    None
 * Compiler:        C++
 * Company:         Copyright (C) 2004 by Microchip Technology, Inc.
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the “Company”) for its PICmicro® Microcontroller is intended and
 * supplied to you, the Company’s customer, for use solely and
 * exclusively on Microchip PICmicro Microcontroller products. The
 * software is owned by the Company and/or its supplier, and is
 * protected under applicable copyright laws. All rights are reserved.
 * Any use in violation of the foregoing restrictions may subject the
 * user to criminal sanctions under applicable laws, as well as to
 * civil liability for the breach of the terms and conditions of this
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 * revision             Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * v0.0.0.0             9/2/04      Implemented MPUSBGetDeviceLink()
 * v1.0.0.0             11/19/04    Original version 1.00 completed
 * v1.0.1.0             03/24/08    Slight update to fix issue which
 *                                  was requiring admin run level to
 *                                  work correctly.
 * v1.1.0.0             05/22/08    Added the support for the
 *                                  following functions:
 *                                   MPUSBSetConfiguration()
 *                                   MPUSBGetDeviceDescriptor()
 *                                   MPUSBGetConfigurationDescriptor()
 *                                   MPUSBGetStringDescriptor()
 *                                  Modified MPUSBGetDLLVersion return
 *                                   format.
 ********************************************************************/

#ifndef _MPUSBAPI_H_
#define _MPUSBAPI_H_

#define	MPUSB_FAIL                  0
#define MPUSB_SUCCESS               1

#define MP_WRITE                    0
#define MP_READ                     1

// MAX_NUM_MPUSB_DEV is an abstract limitation.
// It is very unlikely that a computer system will have more
// then 127 USB devices attached to it. (single or multiple USB hosts)
#define MAX_NUM_MPUSB_DEV           127

///////////////////////////////////////////////////////////////////////////////
//	MPUSBGetDLLVersion : get mpusbapi.dll revision level
//
//	Input:
//		None
//	Output:
//		32-bit revision level MMmmddii
//                 MM - Major release
//                 mm - Minor release
//                 dd - dot release or minor fix
//                 ii - test release revisions
//      Note:
//              The formatting changed between revisions v1.0.1.0 of the
//                driver and v1.1.0.0.  The output of this function was
//                previously MMMMmmmm and did not match the .DLL file
//                numbering format.  The format of this fuction was changed to
//                match how the .DLL file number generation works.
//
DWORD (*MPUSBGetDLLVersion)(void);

///////////////////////////////////////////////////////////////////////////////
//	MPUSBGetDeviceCount : Returns the number of devices with matching VID & PID
//
//	Note that "input" and "output" refer to the parameter designations in calls
//	to this function, which are the opposite of common sense from the
//	perspective of an application making the calls.
//
DWORD (*MPUSBGetDeviceCount)(PCHAR pVID_PID);

///////////////////////////////////////////////////////////////////////////////
//	MPUSBOpen : Returns the handle to the endpoint pipe with matching VID & PID
//
//  All pipes are opened with the FILE_FLAG_OVERLAPPED attribute.
//  This allows MPUSBRead,MPUSBWrite, and MPUSBReadInt to have a time-out value.
//
//  Note: Time-out value has no meaning for Isochronous pipes.
//
//  instance - An instance number of the device to open.
//             Typical usage is to call MPUSBGetDeviceCount first to find out
//             how many instances there are.
//             It is important to understand that the driver is shared among
//             different devices. The number of devices returned by
//             MPUSBGetDeviceCount could be equal to or less than the number
//             of all the devices that are currently connected & using the
//             generic driver.
//
//             Example:
//             if there are 3 device with the following PID&VID connected:
//             Device Instance 0, VID 0x04d8, PID 0x0001
//             Device Instance 1, VID 0x04d8, PID 0x0002
//             Device Instance 2, VID 0x04d8, PID 0x0001
//
//             If the device of interest has VID = 0x04d8 and PID = 0x0002
//             Then MPUSBGetDeviceCount will only return '1'.
//             The calling function should have a mechanism that attempts
//             to call MPUSBOpen up to the absolute maximum of MAX_NUM_MPUSB_DEV
//             (MAX_NUM_MPUSB_DEV is defined in _mpusbapi.h).
//             It should also keep track of the number of successful calls
//             to MPUSBOpen(). Once the number of successes equals the
//             number returned by MPUSBGetDeviceCount, the attempts should
//             be aborted because there will no more devices with
//             a matching vid&pid left.
//
//  pVID_PID - A string containing the PID&VID value of the target device.
//             The format is "vid_xxxx&pid_yyyy". Where xxxx is the VID value
//             in hex and yyyy is the PID value in hex.
//             Example: If a device has the VID value of 0x04d8 and PID value
//                      of 0x000b, then the input string should be:
//                      "vid_04d8&pid_000b"
//
//  pEP      - A string of the endpoint number on the target endpoint to open.
//             The format is "\\MCHP_EPz". Where z is the endpoint number in
//             decimal.
//             Example: "\\MCHP_EP1"
//
//             This arguement can be NULL. A NULL value should be used to
//             create a handles for non-specific endpoint functions.
//             MPUSBRead, MPUSBWrite, MPUSBReadInt are endpoint specific
//             functions.
//             All others are not.
//             Non-specific endpoint functions will become available in the
//             next release of the DLL.
//
//             Note: To use MPUSBReadInt(), the format of pEP has to be
//                   "\\MCHP_EPz_ASYNC". This option is only available for
//                   an IN interrupt endpoint. A data pipe opened with the
//                   "_ASYNC" keyword would buffer the data at the interval
//                   specified in the endpoint descriptor upto the maximum of
//                   100 data sets. Any data received after the driver buffer
//                   is full will be ignored.
//                   The user application should call MPUSBReadInt() often
//                   enough so that the maximum limit of 100 is never reached.
//
//  dwDir    - Specifies the direction of the endpoint.
//             Use MP_READ for MPUSBRead, MPSUBReadInt
//             Use MP_WRITE for MPUSBWrite
//
//  dwReserved Future Use
//
//  Summary of transfer type usage:
//  ============================================================================
//  Transfer Type       Functions                       Time-Out Applicable?
//  ============================================================================
//  Interrupt - IN      MPUSBRead, MPUSBReadInt         Yes
//  Interrupt - OUT     MPUSBWrite                      Yes
//  Bulk - IN           MPUSBRead                       Yes
//  Bulk - OUT          MPUSBWrite                      Yes
//  Isochronous - IN    MPUSBRead                       No
//  Isochronous - OUT   MPUSBWrite                      No
//  ============================================================================
//
//	Note that "input" and "output" refer to the parameter designations in calls
//	to this function, which are the opposite of common sense from the
//	perspective of an application making the calls.
//
HANDLE (*MPUSBOpen)(DWORD instance,         // Input
                 PCHAR pVID_PID,            // Input
                 PCHAR pEP,                 // Input
                 DWORD dwDir,               // Input
                 DWORD dwReserved);         // Input <Future Use>
                 
///////////////////////////////////////////////////////////////////////////////
//	MPUSBRead :
//
//  handle  - Identifies the endpoint pipe to be read. The pipe handle must
//            have been created with MP_READ access attribute.
//
//  pData   - Points to the buffer that receives the data read from the pipe.
//
//  dwLen   - Specifies the number of bytes to be read from the pipe.
//
//  pLength - Points to the number of bytes read. MPUSBRead sets this value to
//            zero before doing any work or error checking.
//
//  dwMilliseconds
//          - Specifies the time-out interval, in milliseconds. The function
//            returns if the interval elapses, even if the operation is
//            incomplete. If dwMilliseconds is zero, the function tests the
//            data pipe and returns immediately. If dwMilliseconds is INFINITE,
//            the function's time-out interval never elapses.
//
//	Note that "input" and "output" refer to the parameter designations in calls
//	to this function, which are the opposite of common sense from the
//	perspective of an application making the calls.
//
DWORD (*MPUSBRead)(HANDLE handle,           // Input
                PVOID pData,                // Output
                DWORD dwLen,                // Input
                PDWORD pLength,             // Output
                DWORD dwMilliseconds);      // Input

///////////////////////////////////////////////////////////////////////////////
//	MPUSBWrite :
//
//  handle  - Identifies the endpoint pipe to be written to. The pipe handle
//            must have been created with MP_WRITE access attribute.
//
//  pData   - Points to the buffer containing the data to be written to the pipe.
//
//  dwLen   - Specifies the number of bytes to write to the pipe.
//
//  pLength - Points to the number of bytes written by this function call.
//            MPUSBWrite sets this value to zero before doing any work or
//            error checking.
//
//  dwMilliseconds
//          - Specifies the time-out interval, in milliseconds. The function
//            returns if the interval elapses, even if the operation is
//            incomplete. If dwMilliseconds is zero, the function tests the
//            data pipe and returns immediately. If dwMilliseconds is INFINITE,
//            the function's time-out interval never elapses.
//
//	Note that "input" and "output" refer to the parameter designations in calls
//	to this function, which are the opposite of common sense from the
//	perspective of an application making the calls.
//
DWORD (*MPUSBWrite)(HANDLE handle,          // Input
                 PVOID pData,               // Input
                 DWORD dwLen,               // Input
                 PDWORD pLength,            // Output
                 DWORD dwMilliseconds);     // Input

///////////////////////////////////////////////////////////////////////////////
//	MPUSBReadInt :
//
//  handle  - Identifies the endpoint pipe to be read. The pipe handle must
//            have been created with MP_READ access attribute.
//
//  pData   - Points to the buffer that receives the data read from the pipe.
//
//  dwLen   - Specifies the number of bytes to be read from the pipe.
//
//  pLength - Points to the number of bytes read. MPUSBRead sets this value to
//            zero before doing any work or error checking.
//
//  dwMilliseconds
//          - Specifies the time-out interval, in milliseconds. The function
//            returns if the interval elapses, even if the operation is
//            incomplete. If dwMilliseconds is zero, the function tests the
//            data pipe and returns immediately. If dwMilliseconds is INFINITE,
//            the function's time-out interval never elapses.
//
//	Note that "input" and "output" refer to the parameter designations in calls
//	to this function, which are the opposite of common sense from the
//	perspective of an application making the calls.
//
DWORD (*MPUSBReadInt)(HANDLE handle,        // Input
                   PVOID pData,             // Output
                   DWORD dwLen,             // Input
                   PDWORD pLength,          // Output
                   DWORD dwMilliseconds);   // Input
                   
///////////////////////////////////////////////////////////////////////////////
//	MPUSBClose : closes a given handle.
//
//	Note that "input" and "output" refer to the parameter designations in calls
//	to this function, which are the opposite of common sense from the
//	perspective of an application making the calls.
//
BOOL (*MPUSBClose)(HANDLE handle);

///////////////////////////////////////////////////////////////////////////////
//	MPUSBGetDeviceDescriptor : Returns the Device Descriptor Data
//
//  handle  - Identifies the endpoint pipe to be read. The pipe handle must
//            have been created with MP_READ access attribute.
//  pDevDsc - pointer to where the resulting descriptor should be copied.
//  dwLen   - the available data in the pDevDsc buffer
//  pLength - a pointer to a DWORD that will be updated with the amount of data
//            actually written to the pDevDsc buffer.  This number will be
//            less than or equal to dwLen.
//
//	Note that "input" and "output" refer to the parameter designations in calls
//	to this function, which are the opposite of common sense from the
//	perspective of an application making the calls.
//
DWORD (*MPUSBGetDeviceDescriptor)(HANDLE handle,       // Input
                               PVOID pDevDsc,       // Output
                               DWORD dwLen,         // Input
                               PDWORD pLength);     // Output

///////////////////////////////////////////////////////////////////////////////
//	MPUSBGetConfigurationDescriptor : Returns the Configuration Descriptor
//
//  handle  - Identifies the endpoint pipe to be read. The pipe handle must
//            have been created with MP_READ access attribute.
//  bIndex  - the index of the configuration descriptor desired.  Valid input
//            range is 1 - 255.
//  pDevDsc - pointer to where the resulting descriptor should be copied.
//  dwLen   - the available data in the pDevDsc buffer
//  pLength - a pointer to a DWORD that will be updated with the amount of data
//            actually written to the pDevDsc buffer.  This number will be
//            less than or equal to dwLen.
//
//	Note that "input" and "output" refer to the parameter designations in calls
//	to this function, which are the opposite of common sense from the
//	perspective of an application making the calls.
//
DWORD (*MPUSBGetConfigurationDescriptor)(HANDLE handle,       // Input
                               UCHAR bIndex,        // Input
                               PVOID pDevDsc,       // Output
                               DWORD dwLen,         // Input
                               PDWORD pLength);     // Output

///////////////////////////////////////////////////////////////////////////////
//	MPUSBGetStringDescriptor : Returns the requested string descriptor
//
//  handle  - Identifies the endpoint pipe to be read. The pipe handle must
//            have been created with MP_READ access attribute.
//  bIndex  - the index of the configuration descriptor desired.  Valid input
//            range is 0 - 255.
//  wLangId - the language ID of the string that needs to be read
//  pDevDsc - pointer to where the resulting descriptor should be copied.
//  dwLen   - the available data in the pDevDsc buffer
//  pLength - a pointer to a DWORD that will be updated with the amount of data
//            actually written to the pDevDsc buffer.  This number will be
//            less than or equal to dwLen.
//
//	Note that "input" and "output" refer to the parameter designations in calls
//	to this function, which are the opposite of common sense from the
//	perspective of an application making the calls.
//
DWORD (*MPUSBGetStringDescriptor)(HANDLE handle,       // Input
                               UCHAR bIndex,        // Input
                               USHORT wLangId,      // Input
                               PVOID pDevDsc,       // Output
                               DWORD dwLen,         // Input
                               PDWORD pLength);     // Output

///////////////////////////////////////////////////////////////////////////////
//	MPUSBSetConfiguration : Sets the device configuration through a USB
//            SET_CONFIGURATION command.
//
//  handle  - Identifies the endpoint pipe to be written. The pipe handle must
//            have been created with MP_WRITE access attribute.
//
//  bConfigSetting
//          - Denotes the configuration number that needs to be set.  If this
//            number does not fall in the devices allowed configurations then
//            this function will return with MP_FAIL
//
//	Note that "input" and "output" refer to the parameter designations in calls
//	to this function, which are the opposite of common sense from the
//	perspective of an application making the calls.
//
DWORD (*MPUSBSetConfiguration)(HANDLE handle,         // Input
                         USHORT bConfigSetting);           // Input
#endif
ich dachte nun, ich müsste wie im ersten Post beschrieben vorgehen, um die dll mit Qt verwenden zu können, aber dem scheint ja nicht so zu sein...

Gruss
Lodo2609

Verfasst: 23. August 2009 12:43
von androphinx
Nein. Wenn du die Dll erstellst, erhälst du neben der Datei eine *.a oder *.lib. Diese gibst du dem Linker mit. Die Headerdatei bindest du dann mit ins Programm ein. Dann sollte der Linker seine Funktionen finden und du brauchst nicht mehr so viel Code zu schreiben.

Mfg androphinx

Verfasst: 23. August 2009 19:57
von lodo2609
Hallo,
ich habe keine lib, bzw. keine lib für Qt sondern nur für den Borland Compiler, die kann ich doch nicht verwenden?

Gruss
Lodo2609