<commit_before>#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <stdio.h>
#include <malloc.h>

#include "../../MemoryModule.h"

typedef int (*addNumberProc)(int, int);

#define DLL_FILE "..\\SampleDLL\\SampleDLL.dll"

void LoadFromFile(void)
{
    addNumberProc addNumber;
    HINSTANCE handle = LoadLibrary(DLL_FILE);
    if (handle == NULL)
        return;

    addNumber = (addNumberProc)GetProcAddress(handle, "addNumbers");
    printf("From file: %d\n", addNumber(1, 2));
    FreeLibrary(handle);
}

void LoadFromMemory(void)
{
    FILE *fp;
    unsigned char *data=NULL;
    size_t size;
    HMEMORYMODULE module;
    addNumberProc addNumber;
    
    fp = fopen(DLL_FILE, "rb");
    if (fp == NULL)
    {
        printf("Can't open DLL file \"%s\".", DLL_FILE);
        goto exit;
    }

    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    data = (unsigned char *)malloc(size);
    fseek(fp, 0, SEEK_SET);
    fread(data, 1, size, fp);
    fclose(fp);

    module = MemoryLoadLibrary(data);
    if (module == NULL)
    {
        printf("Can't load library from memory.\n");
        goto exit;
    }

    addNumber = (addNumberProc)MemoryGetProcAddress(module, "addNumbers");
    printf("From memory: %d\n", addNumber(1, 2));
    MemoryFreeLibrary(module);

exit:
    if (data)
        free(data);
}

int main(int argc, char* argv[])
{
    LoadFromFile();
    printf("\n\n");
    LoadFromMemory();
    return 0;
}
<commit_msg>Use new resource loading API<commit_after>#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <malloc.h>

#include "../../MemoryModule.h"

typedef int (*addNumberProc)(int, int);

#define DLL_FILE "..\\SampleDLL\\SampleDLL.dll"

void LoadFromFile(void)
{
    addNumberProc addNumber;
    HRSRC resourceInfo;
    DWORD resourceSize;
    LPVOID resourceData;
    _TCHAR buffer[100];
    
    HINSTANCE handle = LoadLibrary(DLL_FILE);
    if (handle == NULL)
        return;

    addNumber = (addNumberProc)GetProcAddress(handle, "addNumbers");
    printf("From file: %d\n", addNumber(1, 2));
    
    resourceInfo = FindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
    printf("FindResource returned 0x%p\n", resourceInfo);
    
    resourceSize = SizeofResource(handle, resourceInfo);
    resourceData = LoadResource(handle, resourceInfo);
    printf("Resource data: %ld bytes at 0x%p\n", resourceSize, resourceData);
    
    LoadString(handle, 1, buffer, sizeof(buffer));
    printf("String1: %s\n", buffer);
    
    LoadString(handle, 20, buffer, sizeof(buffer));
    printf("String2: %s\n", buffer);

    FreeLibrary(handle);
}

void LoadFromMemory(void)
{
    FILE *fp;
    unsigned char *data=NULL;
    size_t size;
    HMEMORYMODULE handle;
    addNumberProc addNumber;
    HMEMORYRSRC resourceInfo;
    DWORD resourceSize;
    LPVOID resourceData;
    _TCHAR buffer[100];
    
    fp = fopen(DLL_FILE, "rb");
    if (fp == NULL)
    {
        printf("Can't open DLL file \"%s\".", DLL_FILE);
        goto exit;
    }

    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    data = (unsigned char *)malloc(size);
    fseek(fp, 0, SEEK_SET);
    fread(data, 1, size, fp);
    fclose(fp);

    handle = MemoryLoadLibrary(data);
    if (handle == NULL)
    {
        printf("Can't load library from memory.\n");
        goto exit;
    }

    addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
    printf("From memory: %d\n", addNumber(1, 2));

    resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
    printf("MemoryFindResource returned 0x%p\n", resourceInfo);
    
    resourceSize = MemorySizeofResource(handle, resourceInfo);
    resourceData = MemoryLoadResource(handle, resourceInfo);
    printf("Memory resource data: %ld bytes at 0x%p\n", resourceSize, resourceData);
    
    MemoryLoadString(handle, 1, buffer, sizeof(buffer));
    printf("String1: %s\n", buffer);
    
    MemoryLoadString(handle, 20, buffer, sizeof(buffer));
    printf("String2: %s\n", buffer);

    MemoryFreeLibrary(handle);

exit:
    if (data)
        free(data);
}

int main(int argc, char* argv[])
{
    LoadFromFile();
    printf("\n\n");
    LoadFromMemory();
    return 0;
}

{'fraction_non_alphanumeric': 0.14177693761814744, 'fraction_numerical': 0.00945179584120983, 'mean_word_length': 3.3370901639344264, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4645653', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1406, 'n_words': 327}