简体   繁体   中英

Access violation reading location when accessing a dll method

I am using the following code to access a method in the dll file and I am getting an Access violation reading location 0x41100000 when calling the method from the dll method.

The method that I am trying to access is of the following prototype

 int dstoch(float,float,float,float,float,float,float,float,float);

This is my code

typedef int (*LPMyfunct)(float,float,float,float,float,float,float,float,float);
HINSTANCE hDLL = NULL;
LPMyfunct lpdstoch = NULL;

hDLL = LoadLibrary("c:\\myfile.dll");

if(hDLL!=NULL)
{
    std::cout << "Library loaded \n";
    lpdstoch = (LPMyfunct)GetProcAddress((HMODULE)hDLL, "dstoch");

    int res = 0;
    if(lpdstoch != NULL)
    {
        try
        {
            res = lpdstoch(1.1,2.2,3.3,4.4,5.4,6.4,7.4,8.8,9.9); //Gives the error
        }
        catch (std::exception &e)
        {
            std::cout << e.what();
        }   
    }
}

Any suggestions what the reasons could be ? Any chance there is an error in the dll file ? Is there any way I could read the parameters of the dll file ? Disect it to check if I am getting the parameter types correct ? Decpendency checker shows that the method exists but I cant acertain the argument types ?

Update:

I am still getting the error

First-chance exception at 0x0040356c in test.exe: 0xC0000005: Access violation writing location 0x42080000. Unhandled exception at 0x0040356c in Cexperiment.exe: 0xC0000005: Access violation writing location 0x42080000.

I also believe that this issue might not be related to a calling convention .The reason I believe is because I do not get an error message from VS2010 stating that a calling convention may be a cause.I got that message when i tried using some other dll. So if calling convention or parameters are not an issue (you get the same calling convention message incase the parameters are different) then it might be something else. Any suggestions on what I could try ?

You need to check calling convention of the dll function and declare LPMyfunct type accordingly. Possible values for calling conventions are: stdcall , cdecl , pascal .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM