向VC高手求教,关于控制台程序如何改写成DLL的问题

peters 2005-04-23 03:21:32
这个控制台程序,是可以用visual studio C++.net正常编译的,用于输出硬盘物理序列号的程序。
//diskid32.cpp
#define PRINTING_TO_CONSOLE_ALLOWED

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <windows.h>
#include <winioctl.h>

#include "E:\ntddk\inc\ntddstor.h"



#define TITLE "DiskId32"


char HardDriveSerialNumber [1024];

…………

char * getHardDriveComputerID ()
{
int done = FALSE;
// char string [1024];
__int64 id = 0;
OSVERSIONINFO version;

strcpy (HardDriveSerialNumber, "");

memset (&version, 0, sizeof (version));
version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
GetVersionEx (&version);
if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
// this works under WinNT4 or Win2K if you have admin rights
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf ("\nTrying to read the drive IDs using physical access with admin rights\n");
#endif
done = ReadPhysicalDriveInNTWithAdminRights ();

// this should work in WinNT or Win2K if previous did not work
// this is kind of a backdoor via the SCSI mini port driver into
// the IDE drives
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf ("\nTrying to read the drive IDs using the SCSI back door\n");
#endif
// if ( ! done)
done = ReadIdeDriveAsScsiDriveInNT ();

// this works under WinNT4 or Win2K or WinXP if you have any rights
#ifdef PRINTING_TO_CONSOLE_ALLOWED
printf ("\nTrying to read the drive IDs using physical access with zero rights\n");
#endif
//if ( ! done)
done = ReadPhysicalDriveInNTWithZeroRights ();

}
else
{
// this works under Win9X and calls a VXD
int attempt = 0;

// try this up to 10 times to get a hard drive serial number
for (attempt = 0;
attempt < 10 && ! done && 0 == HardDriveSerialNumber [0];
attempt++)
done = ReadDrivePortsInWin9X ();
}

if (HardDriveSerialNumber [0] > 0)
{
char *p = HardDriveSerialNumber;

//WriteConstantString ("HardDriveSerialNumber", HardDriveSerialNumber);

// ignore first 5 characters from western digital hard drives if
// the first four characters are WD-W
if ( ! strncmp (HardDriveSerialNumber, "WD-W", 4)) p += 5;
for ( ; p && *p; p++)
{
if ('-' == *p) continue;
id *= 10;
switch (*p)
{
case '0': id += 0; break;
case '1': id += 1; break;
case '2': id += 2; break;
case '3': id += 3; break;
case '4': id += 4; break;
case '5': id += 5; break;
case '6': id += 6; break;
case '7': id += 7; break;
case '8': id += 8; break;
case '9': id += 9; break;
case 'a': case 'A': id += 10; break;
case 'b': case 'B': id += 11; break;
case 'c': case 'C': id += 12; break;
case 'd': case 'D': id += 13; break;
case 'e': case 'E': id += 14; break;
case 'f': case 'F': id += 15; break;
case 'g': case 'G': id += 16; break;
case 'h': case 'H': id += 17; break;
case 'i': case 'I': id += 18; break;
case 'j': case 'J': id += 19; break;
case 'k': case 'K': id += 20; break;
case 'l': case 'L': id += 21; break;
case 'm': case 'M': id += 22; break;
case 'n': case 'N': id += 23; break;
case 'o': case 'O': id += 24; break;
case 'p': case 'P': id += 25; break;
case 'q': case 'Q': id += 26; break;
case 'r': case 'R': id += 27; break;
case 's': case 'S': id += 28; break;
case 't': case 'T': id += 29; break;
case 'u': case 'U': id += 30; break;
case 'v': case 'V': id += 31; break;
case 'w': case 'W': id += 32; break;
case 'x': case 'X': id += 33; break;
case 'y': case 'Y': id += 34; break;
case 'z': case 'Z': id += 35; break;
}
}
}
……

int main (int argc, char * argv [])
{
//long id = getHardDriveComputerID ();
char * test;

test = getHardDriveComputerID ();
printf ("\nHello,This is just a test:__________: %s\n", test);
return 0;
}

我想把它改写为一个标准的DLL,供其它程序调用,我现在的做法是建立一个win32 DLL的输出简单符号的模板(用vs c++6.0建立的,然后用vs7.0++打开的,因为在vs7.0++里没有看到建立win32动态链接库项目),然后想把模板中的输出函数改为返回上面程序中的输出函数getHardDriveComputerID的形式以让其它程序调用。但是我只要把这个控制台程序包含进去,就无法编译通过,总是提示在查找预编译指令时,遇到意外的结尾。

下面是原DLL中的主程序:
// HardDiskSN.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "HardDiskSN.h"
#include "diskid32.cpp"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}


// This is an example of an exported variable
HARDDISKSN_API int nHardDiskSN=0;

// This is an example of an exported function.
HARDDISKSN_API int fnHardDiskSN(void)
{
return 42;
}

// This is the constructor of a class that has been exported.
// see HardDiskSN.h for the class definition
CHardDiskSN::CHardDiskSN()
{
return;
}

  
   我是刚接触VC,对这方面还不熟悉,希望高手指点,把控制台程序改为DLL,要如何做才行,非常感谢!
...全文
501 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
peters 2005-04-23
  • 打赏
  • 举报
回复
现在的情况是这样的,如果我在预编译选项里选择,“使用预编译头(/Yu)”,就会出现:
// diskid32.cpp
fatal error C1010: 在查找预编译头指令时遇到意外的文件结尾
如果我选择:
不使用预编译头
则会出现类似于下面的错误:
HardDiskSN error LNK2005: "char * __cdecl ConvertToString(unsigned long * const,int,int)" (?ConvertToString@@YAPADQAKHH@Z) 已经在 diskid32.obj 中定义
HardDiskSN error LNK2005: "char * __cdecl flipAndCodeBytes(char *)" (?flipAndCodeBytes@@YAPADPAD@Z) 已经在 diskid32.obj 中定义
HardDiskSN warning LNK4006: "char * __cdecl getHardDriveComputerID(void)" (?getHardDriveComputerID@@YAPADXZ) 已在 diskid32.obj 中定义;已忽略第二个定义
……
这是什么原因呢?
peters 2005-04-23
  • 打赏
  • 举报
回复
提示是下面这样的:

HardDiskSN error LNK2005: "char * __cdecl ConvertToString(unsigned long * const,int,int)" (?ConvertToString@@YAPADQAKHH@Z) 已经在 diskid32.obj 中定义

HardDiskSN error LNK2005: "char * __cdecl flipAndCodeBytes(char *)" (?flipAndCodeBytes@@YAPADPAD@Z) 已经在 diskid32.obj 中定义

HardDiskSN error LNK2005: "void __cdecl PrintIdeInfo(int,unsigned long * const)" (?PrintIdeInfo@@YAXHQAK@Z) 已经在 diskid32.obj 中定义


HardDiskSN fatal error LNK1169: 找到一个或多个多重定义的符号

……
peters 2005-04-23
  • 打赏
  • 举报
回复
现在我把编译选项中,使用预编译头选项改为不使用预编译头后,如果不把diskid32.cpp包含进来,可以编译成功,但是由于没有包含进来,就没有调用它里面的函数,但包含进来后就出了一大堆很多结构和类型重定义的错误,由于涉及的头文件又多,我不太清楚每个头文件的作用,我搞了一天多了,比较急,有没有朋友愿意帮忙给我看看的,我在QQ上,在线等,谢谢!
oyljerry 2005-04-23
  • 打赏
  • 举报
回复
建一个window dll的工程
把需要的接口给导出,就后就可以让exe等调用了
Practise_Think 2005-04-23
  • 打赏
  • 举报
回复
新建议一个DLL工程,将代码移到这个工程里面,将你要向外界所提供的接口做成EXTERN "C", 所有的接口都显式声明成__stdcall即可.要看有关的例程可以到www.vckbase.com上找!!
peters 2005-04-23
  • 打赏
  • 举报
回复
如果需要上面全部源代码测试,请加我的QQ:51978456

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧