c - 为什么调用 dll 函数会改变我在 C 中的变量?

标签 c vb.net dll

我写这个程序是为了调用 dll 中的一些函数。

一切正常,直到函数接受指针参数。

#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;
}

我得到了这个输出

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

调用rf_request_function后,requestResulttagType的值是正确的,但是,如果我再次打印它们,值将被改变,所以我无法保留结果的值其他变量也有问题,变量 b 不正确。

出现这个问题的原因是什么?是dll问题吗?还是我的程序问题?

我按照这个vb例子在我的程序中声明函数,这个例子带有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

最佳答案

调用约定可能有问题。 DLL 可能具有 __stdcall 函数。您应该将函数指针声明为

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

您的代码中当前发生的是由于调用约定不匹配导致的堆栈损坏。

关于c - 为什么调用 dll 函数会改变我在 C 中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27777692/

相关文章:

c - 使用局部静态变量线程安全/可重入的函数

c - 我想找出我的 C 字符串中有多少个元音和辅音

sql - 序列不包含任何元素 - LINQ、MVC、Average

c++ - 如何使用来自 Haxe 的第三方 dll

c - realloc导致的malloc错误

无法弄清楚如何使用 write() 将整数写入文件

c# - 如何将完整的 C# 项目转换为 vb.net?

c# - .NET 为 Unix 系统(Icinga 或 Nagios)编写文本文件

c++ - 在 .EXE 路径中找不到 DLL

java - 如何将 .dll 导入 Android java 项目(使用 eclipse)