简体   繁体   中英

Why calling dll function will change my variable in C?

I wrote this program to call some function in a dll.

Everything is fine until the function which accepts pointer parameter.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <windows.h>

typedef uint32_t (*rf_init_com)(uint32_t,uint32_t);
typedef uint32_t (*rf_halt)(uint32_t);
typedef uint32_t (*rf_request)(uint16_t, uint8_t, uint16_t*);

int main()
{
    HINSTANCE masterRD = LoadLibrary("MasterRD.dll");
    rf_init_com rf_init_com_function =(rf_init_com) GetProcAddress(masterRD, "rf_init_com");
    rf_halt rf_halt_function =(rf_halt) GetProcAddress(masterRD, "rf_halt");
    rf_request rf_request_function =(rf_request) GetProcAddress(masterRD, "rf_request");


    uint32_t initResult= rf_init_com_function(3,9600);
    printf("init = %d\n",initResult);

    uint32_t haltResult= rf_halt_function(0);
    printf("halt = %d\n",initResult);

    uint16_t tagType=0;
    uint32_t requestResult= rf_request_function(0, 0x52, &tagType);
    printf("request = %d, tag type = %d << first time, value is correct\n",requestResult, tagType);
    printf("request = %d, tag type = %d << second time, value is incorrect\n",requestResult, tagType);
    printf("request = %d, tag type = %d << value is still incorrect\n",requestResult, tagType);
    printf("request = %d, tag type = %d << value is still incorrect\n",requestResult, tagType);
    printf("request = %d, tag type = %d << value is still incorrect\n",requestResult, tagType);
    printf("request = %d, tag type = %d << value is still incorrect\n",requestResult, tagType);



    char *a="abc";
    char *b="xyz";
    printf(a);
    printf(b);
    printf(" << other variable also has problem");


    return 0;
}

I got this output

init = 0
halt = 0
request = 4, tag type = 4 << first time, value is correct
request = 64, tag type = 64 << second time, value is incorrect
request = 64, tag type = 64 << value is still incorrect
request = 64, tag type = 64 << value is still incorrect
request = 64, tag type = 64 << value is still incorrect
request = 64, tag type = 64 << value is still incorrect
abcabc << other variable also has problem

after calling rf_request_function , values of requestResult and tagType are correct but, if I print them again, the values will be changed, so I cannot keep the value of result other variable also has problem, variable b is incorrect.

What is the reason of this problem? Is it dll problem? or my program problem?

I follow this vb example to declare the function in my program, this example come with documentation of dll.

Option Strict Off
Option Explicit On
Module mo_declare
    Public Declare Function rf_init_com Lib "MasterRD.dll" (ByVal port As Integer, ByVal baud As Integer) As Integer

    'int WINAPI rf_halt(unsigned short icdev);
    Public Declare Function rf_halt Lib "MasterRD.dll" (ByVal icdev As Short) As Integer

    'int WINAPI rf_request(unsigned short icdev, unsigned char model, unsigned short *TagType);
    Public Declare Function rf_request Lib "MasterRD.dll" (ByVal icdev As Short, ByVal model As Byte, ByRef TagType As Short) As Integer

End Module

You may have a problem with calling convention. The DLL probably has __stdcall functions. You should declare your function pointers as

typedef uint32_t (__stdcall *rf_init_com)(uint32_t,uint32_t);
...

What currently happen in your code is stack corruption because of mismatching calling convention.

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