-2

SCALE フォルダーにある DLL "sdm00.dll" を呼び出すことができません。パラメーターを asci コード + "SpApp|" に変換された "数値" として受け取る "SpC" 関数を呼び出そうとしていますが、DLL をロードできません &関数を呼び出す.PLZヘルプ

#include <windows.h>  
#include <stdio.h>  
#include <iostream>  

using namespace std;  
typedef string (__cdecl *MYPROC)(string);   

char asc(int a)
{  
    char var;  
    var = (char)a; 
    return var;  
}  

int main()   
{   
    char z;  
    z = asc(20);     
    string str1;  
    string str2;  
    string str3;  
    string retval;     
    str1 = z;  
    str2 = "SpApp|";  
    str3 = str1 + str2;  

    HINSTANCE hinstLib;   
    MYPROC ProcAdd;   
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;  

    // Get a handle to the DLL module

    hinstLib = LoadLibrary(TEXT("c:\\SCALE\\sdm00.dll"));   

    //If the handle is valid, try to get the function address

    if (hinstLib != NULL)   
    { 
        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "SpC"); 

        // If the function address is valid, call the function.

        if (NULL != ProcAdd) 
        {
            fRunTimeLinkSuccess = TRUE;
            cout << "Function is called" << endl;
            string MyRetValue;
            MyRetValue = (string)(ProcAdd)(str3); 
            cout << MyRetValue << endl;
        }
        else
        {
            cout << "Function cannot be called" << endl;
        }

        // Free the DLL module.

        fFreeResult = FreeLibrary(hinstLib); 
    } 

    // If unable to call the DLL function, use an alternative.
    if (!fRunTimeLinkSuccess) 
        printf("Message printed from executable\n"); 

    std::cin.get();
    return 0;
}
4

1 に答える 1

0

DLL がエラーなしで Delphi で使用できる場合、std::stringDelphistd::stringにはstd::string. おそらく、この関数は実際にはchar*入力として a を取り、出力として a を返しますchar*(代わりに、String入力として Delphi を取り、出力として Delphi を返すString場合は、SOL です)。たとえば、次のようになります。

#include <windows.h>  
#include <stdio.h>  
#include <iostream>  

typedef char* (__cdecl *MYPROC)(char*);   

int main()   
{   
    HINSTANCE hinstLib = LoadLibrary(TEXT("c:\\SCALE\\sdm00.dll"));   
    if (hinstLib == NULL)   
    {
        cout << "Unable to load sdm00.dll! Error: " << GetLastError() << std::endl;
    }
    else
    { 
        MYPROC ProcAdd = (MYPROC) GetProcAddress(hinstLib, "SpC"); 
        if (NULL == ProcAdd) 
        {
            cout << "Unable to load SpC() function from sdm00.dll! Error: " << GetLastError() << std::endl;
        }
        else
        {
            std::string str1 = char(20);  
            std::string str2 = "SpApp|";  
            std::string str3 = str1 + str2;  
            std::string retval = ProcAdd(str3.c_str()); 
            std::cout << retval << std::endl;
        }

        FreeLibrary(hinstLib); 
    } 

    std::cin.get();
    return 0;
}
于 2012-12-04T17:21:28.420 に答える