如何修复这些仅在 Visual C++ 中发生而不在 DevC++ 中发生的错误

How can I fix these errors that only happens in Visual C++ but not in DevC++?

本文关键字:错误 DevC++ C++ 何修复 Visual      更新时间:2023-10-16
#include <stdio.h> 
#include <conio.h> 
#include <windows.h>     
typedef short _stdcall (*PtrInp)(short EndPorta);
typedef void _stdcall (*PtrOut)(short EndPorta, short datum);
HINSTANCE hLib;
PtrInp inportB;
PtrOut outportB;
int main()
{
/*Inpout32*/
   //Carrega a DLL na memória.
   hLib = LoadLibrary("inpout32.dll");
   if(hLib == NULL)
   {
       printf("Error.");
      getch();
   }
else {
   inportB = (PtrInp) GetProcAddress(hLib, "Inp32");
   if(inportB == NULL)
   {
      printf("nError2");
   }
   //Obtém o endereço da função Out32 contida na DLL.
   outportB = (PtrOut) GetProcAddress(hLib, "Out32");
   if(outportB == NULL)
   {
      printf("Error3");
   }
}

当我使用 DevC++ 编译时,代码工作正常,但是当我尝试在 Visual C++ 中编译它时,它会给出一堆错误,我该如何修复它们?

输出中显示以下错误:

1><PATH>(12) : error C2059: syntax error : '('
1><PATH>(13) : error C2059: syntax error : '('
1><PATH>(15) : error C2065: 'PtrInp' : undeclared identifier
1><PATH>(15) : error C2146: syntax error : missing ';' before identifier 'inportB'
1><PATH>(15) : error C2065: 'inportB' : undeclared identifier
1><PATH>(16) : error C2065: 'PtrOut' : undeclared identifier
1><PATH>(16) : error C2146: syntax error : missing ';' before identifier 'outportB'
1><PATH>(16) : error C2065: 'outportB' : undeclared identifier
1><PATH>(30) : error C2065: 'inportB' : undeclared identifier
1><PATH>(30) : error C2065: 'PtrInp' : undeclared identifier
1><PATH>(30) : error C2146: syntax error : missing ';' before identifier 'GetProcAddress'
1><PATH>(31) : error C2065: 'inportB' : undeclared identifier
1><PATH>(36) : error C2065: 'outportB' : undeclared identifier
1><PATH>(36) : error C2065: 'PtrOut' : undeclared identifier
1><PATH>(36) : error C2146: syntax error : missing ';' before identifier 'GetProcAddress'
1><PATH>(37) : error C2065: 'outportB' : undeclared identifier
1><PATH>(53) : error C3861: 'outportB': identifier not found
1><PATH>(56) : error C3861: 'outportB': identifier not found
1><PATH>(59) : error C3861: 'outportB': identifier not found
1><PATH>(62) : error C3861: 'outportB': identifier not found
1><PATH>(65) : error C3861: 'outportB': identifier not found
1><PATH>(68) : error C3861: 'outportB': identifier not found
1><PATH>(71) : error C3861: 'outportB': identifier not found
1><PATH>(74) : error C3861: 'outportB': identifier not found
1><PATH>(80) : error C3861: 'outportB': identifier not found

在 MSVC 下,调用约定应放在括号内

typedef short (__stdcall *PtrInp)(short EndPorta);
typedef void (__stdcall *PtrOut)(short EndPorta, short datum);

还请确保在项目设置中使用 MBCS(多字节转换器集)

> 它必须是__stdcall的,即双下划线。除此之外,分类器 __stdcall 应该应用于函数本身,而不是返回值:

typedef short (__stdcall *PtrInp)(short EndPorta);
typedef void (__stdcall *PtrOut)(short EndPorta, short datum);

在此处查看更多示例:http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx