Hey all
i'm stuck in my little project. My plan is to build a application that can interact with a q3-engine game, nothing fancy, i just want to be able to read and send to the game console. magically i managed to actaully create a .exe that does this.. (alltho ive never done c coding before)..
ive used a freeware compiler called dev-c++ (from bloodshed.net) ..anyways..i dont have any references to other compilers.. this just looked easiest.
now, i understand i gotta compile a dll instead of an exe, but i simply doesent know the basics of doing that, i guess you have to declare how you will access the dll..

so i guess the short question is.. how do i compile a DLL from my exisitng code.. in a way so i can call the functions from "outisde"..

now ive tested my exe, and the working code for the .exe is:

#include <windows.h> 
#include <iostream.h> 
#include <string>
using namespace std;
#pragma warning(disable:4129) 
#define PARENTWND "ET Console" 
#define CHILDWND "Edit" 
#define MAX_BUFFER 16384 
#define CR 13 
#define LF 10 
static HWND hwndParent        = NULL; 
static HWND hwndEdit          = NULL; 
static HWND hwndConsole       = NULL; 
char consolestream[16384];
void SetWindowHandles() { 
   hwndParent   = FindWindow(0, PARENTWND); 
   hwndEdit     = FindWindowEx(hwndParent, NULL, CHILDWND, NULL); 
   hwndConsole  = FindWindowEx(hwndParent, NULL, CHILDWND, NULL);    
   hwndConsole  = FindWindowEx(hwndParent, hwndEdit, CHILDWND, NULL); 
} 
 
void SendToET(char *String) { 
   SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)String); 
   SendMessage(hwndEdit, WM_CHAR, CR, 0); 
}
void GetFromET() { 
   SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)sizeof(consolestream), (LPARAM)consolestream); 
} 
int x = 0; 
void doSomething() { 
     x++; 
     switch(x) { 
              case 1: 
               SendMessage(hwndConsole, WM_GETTEXT, (WPARAM)sizeof(consolestream), (LPARAM)consolestream);
               if (consolestream[1] > 0) {
              SendToET("clearviewlog");
                 cout << consolestream << endl; // Prints
                 }
               consolestream[1]= 0; // <-- reset the 3rd char
               break; 
              default: { 
               x = 0; 
               break; 
              } 
     } 
}
int main(){ 
  SetWindowHandles(); 
  setname: 
  Sleep(25); // time in miliseconds between every doSomething 
  doSomething(); // doSomething 
  goto setname; 
  return 0; 
}

from a 3rdpart application i would like to be able to call the two functions called "SendToET" and "GetFromET" ..but i'm stuck at the "compile a proper dll"-point..
the function "SendToET" should just send a string from 3rdpartApp.. and "GetFromET" should just return a string to 3rdpartApp.
right now, this code reads the game console every 25 m/sec, deletes the read data from gameconsole, and then shows the read data in the exe commandline-window, this is how i think it should be, but i just need to further more, be able to call the "SendToET" with custom content/string from 3rdpartApp, and then i finally need the output to be send to 3rdpartApp rather than just be printed in the commandline window.
could anyone with the knowledge spare me a helping hand.. please! :)

Recommended Answers

All 8 Replies

There should be a way of creating a template DLL in Bloodshed DevCP. Have you tried it?
A DLL is nothing but a collection of functions. It does not have a main function.
I dont have any DevC++ specific tutorials but I think you can get an idea by this link. Create an empty DLL template in DevCPP, and try to compile and use these simple samples. You should be able to get your DLL working.

thanx for your reply..

the DLL template in dev-c++ looks like this

dllmain.cpp

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
DllClass::DllClass()
{
}

DllClass::~DllClass ()
{
}

BOOL APIENTRY DllMain (HINSTANCE hInst     /* Library instance handle. */ ,
                       DWORD reason        /* Reason this function is being called. */ ,
                       LPVOID reserved     /* Not used. */ )
{
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;
      case DLL_PROCESS_DETACH:
        break;
      case DLL_THREAD_ATTACH:
        break;
      case DLL_THREAD_DETACH:
        break;
    }
    /* Returns TRUE on success, FALSE on failure */
    return TRUE;
}

dll.h

#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

class DLLIMPORT DllClass
{
  public:
    DllClass();
    virtual ~DllClass(void);
  private:
};

#endif /* _DLL_H_ */

now how do i mix that up with my own code?

I guess the functions should look like this. I see that you are using global variables. If you are trying to use a dll, avoid using global variables. Since you can't be certain how many processes access these variables, you can't be sure when these variables change value, and will give you unpredictable results.
So design your functions so that they will be using no global variables.
dll.h

#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

DLLIMPORT void SendToET(char *String);
DLLIMPORT void GetFromET();

#endif /* _DLL_H_ */

dll.cpp

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>

void SendToET(char *String)
{ 
   SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)String); 
   SendMessage(hwndEdit, WM_CHAR, CR, 0); 
}
void GetFromET()
 { 
   SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)sizeof(consolestream), (LPARAM)consolestream); 
}

ohh..its almost working now.. amazing.. i never thought it would work ...now i can use the SendToET .. but the GetFromET() function is wrong.. ill have to come up with something.. anyways..the hwndEdit should be hwndconsole ..

but ill try..thnx

i have probles returning the console strings.. in the variable consolestream.. it seems i cant do

SendMessage(hwndConsole, WM_GETTEXT, (WPARAM)sizeof(consolestream), (LPARAM)consolestream);
               if (consolestream[1] > 0) {
              SendToET("clearviewlog");
                 cout << consolestream << endl; // Prints
                 return consolestream;

it gives me: error: return-statement with a value, in function returning 'void'

So isnt it obvious? If the return type of a function is void , that means it is not returning anything. If you want to return consolestream , make the return type of the function the same as the data type of controlstream .

Change void GetFromET() to char* GetFromET() . These are the C/C++ basics. What are you doing writing Windows applications without getting the basics right?

Wolfpack, i'm very sorry.. but yes. as i wrote this IS the first c code app i ever messed with.. i like to see my self as a "quick learner" ..but youre absolutely right..i have close to no idea about c coding.. i know general things like; for, if, vars , functions, ararys, ..stuff like that..but the fact is ..i'm very close to my goal..thanx to you... so thank you very much for helping out a total noob like me.

edit: just got home from dinner..will try what you suggested.

It' all working perfect now, i got the strings parsed over in my java/AS environment.. and from there i know what i'm doing ;)

Thanx Wolfpack, couldnt have done it without ur help.

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.