VC++6.0使用缺失的库文件
由于SDK版本过于陈旧,VC++6.0很多库文件没有,需要到VS2008或2010
中找,但是如果一定要使用VC6来编程的话有一个很好的解决办法,就是使用
动态链接库。具体步骤:
1、在VS环境中将需要使用的库函数先前调用,然后编译成DLL文件。
2、在VC中动态链接就可以了。
用个例子来说明,假设我们要使用两个数加法函数,而VC6中没有实现这一函
数的库文件,那么现在VS中写一个加法的导出函数,再在VC中调用这个导出函数
一、DLL文件的生成
建立一个WIN32的DLL工程取名为Win32DLL,包含所需库文件,导出函数如下:
stafx.h文件如下(系统自动生成)
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__7A23F8E0_37A1_44C5_9FE5_91B46C430BF3__INCLUDED_)
#define AFX_STDAFX_H__7A23F8E0_37A1_44C5_9FE5_91B46C430BF3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Insert your headers here
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__7A23F8E0_37A1_44C5_9FE5_91B46C430BF3__INCLUDED_)
stafx.cpp(系统自动生成)
// stdafx.cpp : source file that includes just the standard includes // Win32Dll.pch will be the pre-compiled header // stdafx.obj will contain the pre-compiled type information #include "stdafx.h" // TODO: reference any additional headers you need in STDAFX.H // and not in this file
Win32Dll.h
// The following ifdef block is the standard way of creating macros which make exporting // from a DLL simpler. All files within this DLL are compiled with the WIN32DLL_EXPORTS // symbol defined on the command line. this symbol should not be defined on any project // that uses this DLL. This way any other project whose source files include this file see // WIN32DLL_API functions as being imported from a DLL, wheras this DLL sees symbols // defined with this macro as being exported. #ifdef WIN32DLL_EXPORTS #define WIN32DLL_API __declspec(dllexport) #else #define WIN32DLL_API __declspec(dllimport) #endif extern "C" WIN32DLL_API int add(int a, int b);
Win32Dll.cpp
// Win32Dll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "Win32Dll.h"
WIN32DLL_API int add(int a, int b)
{//此处可以添加所需的库函数
return a+b; }
注意在函数申明前一定要加extern “C” 使其能在C++程序中调用
这样就可以使用下面的方法调用函数了
#include <iostream.h>
#include <stdio.h>
#include <windows.h>
int main()
{
typedef int(addFunc)(int a, int b);
addFunc *pAdd = 0;
//加载dll
HINSTANCE hDLLDrv;
hDLLDrv = LoadLibrary("Win32Dll.dll");
//获取函数的指针
if(hDLLDrv)
{
pAdd = (addFunc *)GetProcAddress(hDLLDrv, "add");
}
if(pAdd!=NULL)
{
cout<<pAdd(1,2)<<endl;
}
FreeLibrary(hDLLDrv);
return 0;
}

浙公网安备 33010602011771号