Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading text files in a dll file

The Setup

Microsoft Visual Studio Professional 2015, running on Windows 10 Pro
Unity 5.3.1f1 (x64)
The project is built upon an example project provided by Unity on their site. The project can be found here.


That which needs to be done

I'm looking into creating a opengl plugin (in the form of a dll) for use with Unity. In their example code the vertex and fragment shaders are hard coded in the code like so:

#define VPROG_SRC(ver, attr, varying)                               \
    ver                                                             \
    attr " highp vec3 pos;\n"                                       \
    attr " lowp vec4 color;\n"                                      \
    "\n"                                                            \
    varying " lowp vec4 ocolor;\n"                                  \
    "\n"                                                            \
    "uniform highp mat4 worldMatrix;\n"                             \
    "uniform highp mat4 projMatrix;\n"                              \
    "\n"                                                            \
    "void main()\n"                                                 \
    "{\n"                                                           \
    "   gl_Position = (projMatrix * worldMatrix) * vec4(pos,1);\n"  \
    "   ocolor = color;\n"                                          \
    "}\n"                                                           \

I don't like this approach and I don't need it's flexibility. So I wanted to put each shader in their own file and load the file using the following piece of code.

const char* ShaderLoader::ReadShader(char *filename)
{
    std::string shaderCode;
    std::ifstream file(filename, std::ios::in);

    if (!file.good())
    {
        std::cout << "Can't read file " << filename << std::endl;
        return nullptr;
    }

    file.seekg(0, std::ios::end);
    shaderCode.resize((unsigned int)file.tellg());
    file.seekg(0, std::ios::beg);
    file.read(&shaderCode[0], shaderCode.size());
    file.close();

    return shaderCode.c_str();
}

The Problem

The output of my project is a .dll file and I wanted my two shaders included in that file, where they could be loaded. Maybe this is not possible, I don't know.
I've added the shaders to the project. I've also right clicked each file and chosen Include In Project.

I don't where the two files end up. Are they included in the dll file and if so what path do I need to pass to ShaderLoader::ReadShader to read them?

I've tried to figure that out by using GetModuleFileName and FindFirstFile but to no success.

So ultimately the question is: Is it possible to include text files in dll libraries built with Visual Studio and if so how do I go about it?

like image 692
Bjorninn Avatar asked Oct 31 '22 07:10

Bjorninn


1 Answers

Of course it will work to read the file from the DLL, but not using your code. The problem here is that you return a pointer to data that will disappear when the function returns.

The variable shaderCode is a local variable inside the ReadShader function, so it will of course be destructed once the function returns. That destruction includes freeing memory used for the contained string, so the pointer you return is pointing to data no longer allocated and trying to use that data will lead to undefined behavior.

Instead return the actual std::string object and get the pointer when actually needed. Alternatively (but not really something I recommend) allocate memory yourself and copy the string into the memory, and return a pointer to that.

like image 88
Some programmer dude Avatar answered Nov 15 '22 07:11

Some programmer dude