Skip to content

Commit

Permalink
bugfix: print error
Browse files Browse the repository at this point in the history
  • Loading branch information
JaysonAlbert committed May 4, 2018
1 parent 4ab1aaf commit 4c76613
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tdx_api/tdx_api.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>C:\local\boost_1_64_0\lib32-msvc-14.1;D:\Users\jayso\Anaconda2\libs\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>C:\Program Files (x86)\Windows Kits\10\Lib\10.0.15063.0\um\x86\;C:\local\boost_1_64_0\lib32-msvc-14.1;D:\Users\jayso\Anaconda2\libs\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down
53 changes: 52 additions & 1 deletion tdx_api/tdx_instance.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,65 @@
#include "stdafx.h"
#include "tdx_instance.h"
#include "consts.h"
#include "Shlwapi.h"

#include <windows.h>
#include <strsafe.h>

#include <string>
#include <stdio.h>

void print_error() {
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);

using namespace std;
// Display the error message and exit the process

lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("failed with error %d: %s"),
dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}

void TdxInstance::Open(string dll_name)
{
module_ = LoadLibrary(dll_name.c_str());

if (module_ == NULL) {
print_error();
printf("failed to load dll %s\n", dll_name.c_str());
char pwd[MAX_PATH];
char path[MAX_PATH];
GetCurrentDirectory(MAX_PATH, pwd);
PathCombine((char*)path, (char*)pwd, (char*)dll_name.c_str());

module_ = LoadLibrary(path);
if(module_ == NULL){
printf("failed to load dll %s\n", path);
print_error();
exit(-1);
}

}

//获取api函数
OpenTdx = (OpenTdxDelegate)GetProcAddress(module_, "OpenTdx");
CloseTdx = (CloseTdxDelegate)GetProcAddress(module_, "CloseTdx");
Expand Down

0 comments on commit 4c76613

Please sign in to comment.