向VC高手求教,关于控制台程序如何改写成DLL的问题
这个控制台程序,是可以用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,要如何做才行,非常感谢!