2015-01-05 2 views
0

나는 dll에서 어떤 함수를 호출하기 위해이 프로그램을 작성했다.dll 함수를 호출하면 C에서 변수가 변경됩니다.

포인터 매개 변수를 허용하는 기능까지는 문제가 없습니다. 내가 rf_request_function를 호출 한 후이 출력

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 

있어

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

, 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 

답변

2

호출 규칙에 문제가있을 수 있습니다. DLL에는 아마도 __stdcall 함수가 있습니다. 함수 포인터를 다음과 같이 선언해야합니다.

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

코드에서 현재 발생하는 현상은 호출 규칙이 일치하지 않아 스택이 손상된 것입니다.

+0

많은 호출 규칙이 있다는 것을 발견했습니다. 어떻게 호출 규칙이 무엇인지 알 수 있습니까? 방금 dll 파일을 가지고 있다면? –

+0

@CLSo DLL 만있는 경우 CFF Explorer와 같은 PE 편집기/뷰어를 사용하여 내 보낸 함수 이름을 볼 수 있습니다. 내 보낸 함수 이름은 규칙에 따라 특별한 형식을 갖습니다 (목록 [http://enixwiz.net/techtips/win32-callconv.html], 여기에는 데코 레이팅 된 이름이있는 차트가 있음). 끔찍한 형식의 이름을 발견하면 아마도 C++ 이름 맹 글링 일 것입니다. – ElderBug

+0

@CLSo 또한 이름 형식이 때로는 정확하지 않을 수 있으며 가장 빠른 방법은 작동 할 때까지 모든 규칙을 테스트하는 것입니다. – ElderBug

관련 문제