Typconversation von C Funktion scheitert



  • Ich habe folgenden Code aus einer vorgebenen C Datei, den ich mit C++ kompilieren möchte:

    vorgegeben:

    int (__stdcall *MMC_COM_open_Ptr)(int PortNumber, int baudrate);
    MMC_COM_open_Ptr = (void*) GetProcAddress(DLLHandle, "MMC_COM_open");
    

    ich bekomme die Fehlermeldung:

    1>e:\daten\dev\cpp\hibiscus\positioningcontrol\positioningcontrol\positioningcontrol\pi_c862mercury.cpp(31) : error C2440: '=': 'void *' kann nicht in 'int (__stdcall *)(int,int)' konvertiert werden
    1>        Konvertierung von 'void*' in Zeiger auf nicht-'void' erfordert eine explizite Typumwandlung
    

    Was mache ich damit jetzt?

    Matthias



  • typedef int (__stdcall *MMC_COM_open_Ptr_t)(int PortNumber, int baudrate);
    MMC_COM_open_Ptr_t MMC_COM_open_Ptr;
    MMC_COM_open_Ptr = (MMC_COM_open_Ptr_t) GetProcAddress(DLLHandle, "MMC_COM_open");
    

    Syntax ohne Gewähr, sollte aber passen.



  • Wenn de schon C++ hast, nimm einen reinterpret_cast ^^

    typedef int(__stdcall*fn_mmc_com_open)(int, int);
    
    ::HINSTANCE hDllMMCCom(::LoadLibrary(TEXT("mmc.dll"));
    if (hDllMMCCom == NULL) throw std::runtime_error("could not load required programm part!");
    
    fn_mmc_com_open mmc_com_open = reinterpret_cast<fn_mmc_com_open>(::GetProcAddress(hDllMMCCom, TEXT("MMC_COM_open"));
    if (mmc_com_open == NULL) throw std::runtime_error("invalid dll \"mmc.dll\"");
    mmc_com_open(10, 100);
    
    ::FreeLibrary(hDllMMCCom);
    


  • Welchen Vorteil hat den ein

    ::HINSTANCE
    

    gegenüber einem

    HINSTANCE
    

    Matthias



  • (D)Evil schrieb:

    typedef int(__stdcall*fn_mmc_com_open)(int, int);
    
    fn_mmc_com_open mmc_com_open = reinterpret_cast<fn_mmc_com_open>(::GetProcAddress(hDllMMCCom, TEXT("MMC_COM_open"));
    

    bislang weise ich die funktion wie folgt zu

    int __stdcall MMC_COM_open(int PortNumber, int baudrate) { return (*MMC_COM_open_Ptr)(PortNumber, baudrate);}
    

    Wie passt das zu deiner Definition?
    Geht auch ein

    typedef int(__stdcall*fn_mmc_com_open)(int PortNumber, int baudrate);
    fn_mmc_com_open mmc_com_open
    

    in der .h Datei und folgendes in der .cpp Datei?

    mmc_com_open = reinterpret_cast<fn_mmc_com_open>(::GetProcAddress(hDllMMCCom, TEXT("MMC_COM_open"));
    

    Matthias


Anmelden zum Antworten