編輯:
我重新編譯爲庫爲C的源,並且該固定它。
我有我需要在我的應用程序中使用此代碼。這是爲了寫入串行端口,我不知道如何讓它在C中運行。我有一個C++版本,以及一個看起來更像C的版本,用於編譯Borland C++ 5.5編譯器,但我不能讓它在那裏或在我的項目中編譯。
編輯:我應該注意,它編譯(和鏈接)當我編譯爲c + +,但不是當我編譯爲c。
這裏的鏈接錯誤我得到:
1>InpoutTest.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main
1>InpoutTest.obj : error LNK2019: unresolved external symbol [email protected] referenced in function _main
這裏的C++代碼。我不需要命令行功能,我只需要能夠調用Out32()。我甚至不需要閱讀。
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
/* ----Prototypes of Inp and Outp--- */
short _stdcall Inp32(short PortAddress);
void _stdcall Out32(short PortAddress, short data);
/*--------------------------------*/
int main(int argc, char* argv[])
{
int data;
if(argc<3)
{
//too few command line arguments, show usage
printf("Error : too few arguments\n\n***** Usage *****\n\nInpoutTest read <ADDRESS> \nor \nInpoutTest write <ADDRESS> <DATA>\n\n\n\n\n");
}
else if(!strcmp(argv[1],"read"))
{
data = Inp32(atoi(argv[2]));
printf("Data read from address %s is %d \n\n\n\n",argv[2],data);
}
else if(!strcmp(argv[1],"write"))
{
if(argc<4)
{
printf("Error in arguments supplied");
printf("\n***** Usage *****\n\nInpoutTest read <ADDRESS> \nor \nInpoutTest write <ADDRESS> <DATA>\n\n\n\n\n");
}
else
{
Out32(atoi(argv[2]),atoi(argv[3]));
printf("data written to %s\n\n\n",argv[2]);
}
}
return 0;
}
這裏的其他樣本:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
/* Definitions in the build of inpout32.dll are: */
/* short _stdcall Inp32(short PortAddress); */
/* void _stdcall Out32(short PortAddress, short data); */
/* prototype (function typedef) for DLL function Inp32: */
typedef short _stdcall (*inpfuncPtr)(short portaddr);
typedef void _stdcall (*oupfuncPtr)(short portaddr, short datum);
int main(void)
{
HINSTANCE hLib;
inpfuncPtr inp32;
oupfuncPtr oup32;
short x;
int i;
/* Load the library */
hLib = LoadLibrary("inpout32.dll");
if (hLib == NULL) {
printf("LoadLibrary Failed.\n");
return -1;
}
/* get the address of the function */
inp32 = (inpfuncPtr) GetProcAddress(hLib, "Inp32");
if (inp32 == NULL) {
printf("GetProcAddress for Inp32 Failed.\n");
return -1;
}
oup32 = (oupfuncPtr) GetProcAddress(hLib, "Out32");
if (oup32 == NULL) {
printf("GetProcAddress for Oup32 Failed.\n");
return -1;
}
/***************************************************************/
/* now test the functions */
/* Try to read 0x378..0x37F, LPT1: */
for (i=0x378; (i<0x380); i++) {
x = (inp32)(i);
printf("port read (%04X)= %04X\n",i,x);
}
/***** Write the data register */
i=0x378;
x=0x77;
(oup32)(i,x);
printf("port write to 0x%X, datum=0x%2X\n" ,i ,x);
/***** And read back to verify */
x = (inp32)(i);
printf("port read (%04X)= %04X\n",i,x);
/***** One more time, different value */
i=0x378;
x=0xAA;
(oup32)(i,x);
printf("port write to 0x%X, datum=0x%2X\n" ,i ,x);
/***** And read back to verify */
x = (inp32)(i);
printf("port read (%04X)= %04X\n",i,x);
FreeLibrary(hLib);
return 0;
}
任何幫助,將不勝感激。
你得到什麼編譯器錯誤? – 2010-06-29 15:56:47
這兩個看起來都非常像直C語言。問題可能是您的C編譯器不理解_stdcall關鍵字的含義。你使用什麼編譯器?而且,正如傑夫所問,你得到了什麼錯誤? – 2010-06-29 16:00:38
順便提一句,這兩個程序之間的主要區別在於第一次鏈接到DLL(在鏈接時),第二次使用LoadLibrary在運行時加載庫。 – 2010-06-29 16:06:39