我有一个使用 DLL 加密文件的程序。我想编写一个使用 DLL 的程序,以便我也可以使用相同的算法加密文件。但是,我是使用 DLL 的新手,所以我遇到了问题。
我做了一些研究,得到了一些几乎可以工作的东西,但我认为我尝试使用的 DLL 有其他依赖项,我也不知道如何正确加载它们。
这是我现在的代码:
#include "stdafx.h"
#include <conio.h> // Header file containing getch() prototype
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
typedef string(*CRYPT)(string);
int _tmain(int argc, _TCHAR* argv[])
{
CRYPT encrypt;
HMODULE hMod; // handle to loaded library module
BOOL bRes; // BOOL to check if DLL was successfully unloaded
// returns a handle to the DLL, otherwise NULL
hMod = LoadLibraryA("ChangedName.dll");
if (hMod == NULL)
{
cout << "NULL" << endl;
system("PAUSE");
return 1;
}
// returns the address of the DLL functions, otherwise NULL
encrypt = (CRYPT)GetProcAddress(hMod, "Encrypt");
// function call
string enc = (encrypt)("this is a string");
cout << "Encrypted: " << enc << endl;
cout << "Press a key to exit" << endl;
_getch();
bRes = FreeLibrary(hMod);
return 0;
}
运行时,我得到:
'Project1.exe' (Win32): Loaded 'C:\Users\REMOVED\Documents\Visual Studio 2013\Projects\Project1\Debug\Project1.exe'. Symbols loaded.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Users\REMOVED\Documents\Visual Studio 2013\Projects\Project1\Debug\L2TowerUtils.dll'. Module was built without symbols.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\psapi.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ole32.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleaut32.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr110.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp110.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\mscoree.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\combase.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll'. Cannot find or open the PDB file.
'Project1.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shlwapi.dll'. Cannot find or open the PDB file.
First-chance exception at 0x00000000 in Project1.exe: 0xC0000005: Access violation executing location 0x00000000.
Unhandled exception at 0x7791BDA1 in Project1.exe: 0xC0000005: Access violation executing location 0x00000000.
错误发生在string enc = (encrypt)("this is a string");