hi, im getting this error:

Unhandled exception at 0x00c11005 in bnetconnection.exe: 0xC0000005: Access violation reading locaiton 0x00c11005

in this particular instance, what i got from the debugger is that 0x00c11005 is my _ProcessCommands function

dll:

#include <iostream>

using namespace std;

extern "C" _declspec(dllexport) void ProcessCommands( const char*, const char*, const char*, const char* );

__declspec(dllexport) void ProcessCommands( const char* client, const char* location, const char* username, const char* message )
{
    cout << "DLEAGUE: " << client << " " << location << " " << username << " " << message << endl;
}

main files:

#ifndef PLUGINS_H
#define PLUGINS_H

#include "main.h"
#include "config.h"

typedef void ( *ProcessCommands ) ( const char*, const char*, const char*, const char* );

class CPlugins
{
private:
public:
    CPlugins( );
    CPlugins( CFileConfig *CFG, string strPrefix );
    vector<string> m_Plugins;

    ProcessCommands _ProcessCommands; // sent to plugins when a chat message is received on any client
};

#endif // PLUGINS_H
else
                {
                    for( size_t j = 0; j < botPlugins[i].m_Plugins.size( ); j++ )
                    {
                        HINSTANCE hInstLibrary = LoadLibrary( botPlugins[i].m_Plugins[j].c_str( ) );
                         
                        if( hInstLibrary )
                        {
                            cout << "[IRC: " << ircConfig[i].m_Nickname << "] " << botPlugins[i].m_Plugins[j] << " successfully loaded" << endl;

                            botPlugins[i]._ProcessCommands = ( ProcessCommands ) GetProcAddress( hInstLibrary, "ProcessCommands" );

                            FreeLibrary( hInstLibrary );
                        }
                        else
                        {
                            cout << "[IRC: " << ircConfig[i].m_Nickname << "] " << botPlugins[i].m_Plugins[j] << " failed to load: " << GetLastError( ) << endl;
                        }
                    }
                }
cout << "[IRC: " << m_IRCConfig->m_Nickname << "] <" << packetInfo->m_Username << "> " << packetInfo->m_Message << endl;
        if( botPlugins->_ProcessCommands )
        {
            const char * channel = m_IRCConfig->m_Channel.c_str( );
            const char * Username = packetInfo->m_Username.c_str( );
            const char * Message = packetInfo->m_Message.c_str( );
            botPlugins->_ProcessCommands( "IRC", "fff", "uuuu", "ddd" );
        }

it loads the dll fine and finds the function but when it calls the function i just get that error, ive tried using the variables shown in the last snip and some constant values but it's the same error

It might be possible that the DLL function throws an exception, and exceptions cannot cross module boundaries (you cannot throw an exception in a DLL and catch it in the exe). That could be the source of the problem. Maybe you can try to wrap the entire function ProcessCommand with a general catch statement, as follows:

__declspec(dllexport) void ProcessCommands( const char* client, const char* location, const char* username, const char* message )
{
  try {
    cout << "DLEAGUE: " << client << " " << location << " " << username << " " << message << endl;
  } catch(...) { }; //if an exception occurs, nothing will happen or get out of the function call.
}

If it works, you still have to figure out why there is an exception (I have never heard of "cout <<" throwing an exception, but who knows.. that's all I can think of).

N.B.: those three dots "..." are intentional, it tells the compiler to catch anything (the three dots are called the ellipsis parameter.. one of the useful oddities of C++).

same thing

First-chance exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
Unhandled exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.

the points to the same line if you were looking for something different, when i hit continue it just infinite loops between the errors:

First-chance exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
Unhandled exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
First-chance exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
Unhandled exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
First-chance exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
Unhandled exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
First-chance exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
Unhandled exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
First-chance exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
Unhandled exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
First-chance exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.
Unhandled exception at 0x00c11050 in bnetconnection.exe: 0xC0000005: Access violation reading location 0x00c11050.

don't know if it means anything but i don't know what other information to add

Holy shit! I just say the mistake... I don't know why I didn't see that earlier. In this piece of code:

HINSTANCE hInstLibrary = LoadLibrary( botPlugins[i].m_Plugins[j].c_str( ) );
                         
                        if( hInstLibrary )
                        {
                            cout << "[IRC: " << ircConfig[i].m_Nickname << "] " << botPlugins[i].m_Plugins[j] << " successfully loaded" << endl;

                            botPlugins[i]._ProcessCommands = ( ProcessCommands ) GetProcAddress( hInstLibrary, "ProcessCommands" );

                            FreeLibrary( hInstLibrary );
                        }
                        else

You cannot call FreeLibrary and expect to be able to call any functions from that DLL afterwards. You need to call FreeLibrary only at the very end, when you will not call the DLL function again. Otherwise you get an access violation if you try and call it.

:)

from what i seen in the examples i just assumed it stored the functions into memory

thanks

>>from what i seen in the examples i just assumed it stored the functions into memory

For the record, LoadLibrary puts all the DLL's code into RAM memory, but it is not part of the applications memory directly (because the DLL is shared by all applications that needs it). Then, GetProcAddress gives you the address of a function within the DLL's code in RAM. Finally, calling FreeLibrary will release the memory for the DLL's code (unless another application is still using it). So, after FreeLibrary is called that address you got before from GetProcAddress has no more meaning and points nowhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.