GitBook: [master] one page modified

This commit is contained in:
CPol 2021-03-05 12:05:46 +00:00 committed by gitbook-bot
parent be72ccc8f4
commit e71d23e868
No known key found for this signature in database
GPG Key ID: 07D2180C7B12D0FF
1 changed files with 5 additions and 5 deletions

View File

@ -97,7 +97,7 @@ In my other articles, I always skipped the development part because I assumed th
Here is the initial code generated by Visual Studio:
```text
```c
#include <iostream>
int main()
@ -108,7 +108,7 @@ int main()
Of course, thats not what we want. We want to create a DLL, not an EXE, so we have to replace the `main` function with `DllMain`. You can find a skeleton code for this function in the documentation: [Initialize a DLL](https://docs.microsoft.com/en-us/cpp/build/run-time-library-behavior#initialize-a-dll).
```text
```c
#include <Windows.h>
extern "C" BOOL WINAPI DllMain(HINSTANCE const instance, DWORD const reason, LPVOID const reserved)
@ -133,7 +133,7 @@ In parallel, we also need to change the settings of the project to specify that
Next, I add my custom log helper function.
```text
```c
#include <Lmcons.h> // UNLEN + GetUserName
#include <tlhelp32.h> // CreateToolhelp32Snapshot()
#include <strsafe.h>
@ -198,7 +198,7 @@ void Log(LPCWSTR pwszCallingFrom)
Then, we can populate the DLL with the three functions we saw in the documentation. The documentation also states that they should return `ERROR_SUCCESS` if successful.
```text
```c
DWORD APIENTRY OpenPerfData(LPWSTR pContext)
{
Log(L"OpenPerfData");
@ -220,7 +220,7 @@ DWORD APIENTRY ClosePerfData()
Ok, so the project is now properly configured, `DllMain` is implemented, we have a log helper function and the three required functions. One last thing is missing though. If we compile this code, `OpenPerfData`, `CollectPerfData` and `ClosePerfData` will be available as internal functions only so we need to **export** them. This can be achieved in several ways. For example, you could create a [DEF](https://docs.microsoft.com/en-us/cpp/build/exporting-from-a-dll-using-def-files) file and then configure the project appropriately. However, I prefer to use the `__declspec(dllexport)` keyword \([doc](https://docs.microsoft.com/en-us/cpp/build/exporting-from-a-dll-using-declspec-dllexport)\), especially for a small project like this one. This way, we just have to declare the three functions at the beginning of the source code.
```text
```c
extern "C" __declspec(dllexport) DWORD APIENTRY OpenPerfData(LPWSTR pContext);
extern "C" __declspec(dllexport) DWORD APIENTRY CollectPerfData(LPWSTR pQuery, PVOID* ppData, LPDWORD pcbData, LPDWORD pObjectsReturned);
extern "C" __declspec(dllexport) DWORD APIENTRY ClosePerfData();