steambird1 / MinServer

A C++ Web server.
MIT License
0 stars 0 forks source link

Runtime error when using my own DLL extension #10

Closed steambird1 closed 2 years ago

steambird1 commented 2 years ago

I wrote this to run VBS file:

#define _CRT_NON_CONFORMING_SWPRINTFS
#define _CRT_SECURE_NO_WARNINGS

#include <cstdlib>
#include "c_framework.h"
using namespace std;

extern "C" {

    __declspec(dllexport)
    BOOL FindFirstFileExists(LPCSTR lpPath, DWORD dwFilter)
    {
        WIN32_FIND_DATA fd;
        HANDLE hFind = FindFirstFileA(lpPath, &fd);
        BOOL bFilter = (FALSE == dwFilter) ? TRUE : fd.dwFileAttributes & dwFilter;
        BOOL RetValue = ((hFind != INVALID_HANDLE_VALUE) && bFilter) ? TRUE : FALSE;
        FindClose(hFind);
        return RetValue;
    }

    // Is file exists?
    __declspec(dllexport)
    BOOL FilePathExists(LPCSTR lpPath)
    {
        return FindFirstFileExists(lpPath, FALSE) && (!FindFirstFileExists(lpPath, FILE_ATTRIBUTE_DIRECTORY));
    }

    __declspec(dllexport)
        send_info AssiocateMain(cc_str request, cc_str req_path, asdata aslib) {
        constexpr char tmpl[] = { "cmd /c start \"MinServer WScript\" wscript " };
        char *cmd = (char*)aslib.mc_lib.m_alloc(strlen(tmpl) + MAX_PATH + 20);
        constexpr char success[] = { "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 7\n\nSuccess" };
        constexpr char err[] = { "HTTP/1.1 404 Not Found\nContent-Type: text/plain\nContent-Length: 14\n\nFile not found" };
        if (FilePathExists(req_path)) {
            sprintf(cmd, "%s%s", tmpl, req_path);
            return { (int)strlen(success), success };
        }
        else {
            return { (int)strlen(err),err };
        }
        }
}

But it causes runtime error in the server.

steambird1 commented 2 years ago

Already I even didn't add system() call...

steambird1 commented 2 years ago

Try use less mc_lib.m_alloc() and make sure "Multi-thread DLL" selected in VS compile options.

steambird1 commented 2 years ago

Also you can try latest version; here is a program starter. you can add "assoc" to use that instead.

steambird1 commented 2 years ago

Well, you didn't use

extern "C" {
//...
}

at all so older version of MinServer won't get your function and crash; latest version of MinServer will reports no vaild DLL entry.