Jump to content

Making DLLs for use with AutoIt.


Recommended Posts

I've been trying to make a DirectX wrapper to be used with AutoIt or make a wrapper for a graphics engine/physics/what ever, but the functions don't work and i tried to use the plugin SDK didn't go so well.....So here's what i did:

#include <d3d9.h>

#pragma comment(lib,"d3d9.lib")
LPDIRECT3D9       g_pD3D       = NULL;
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;

HRESULT InitialiseD3D(HWND hWnd)
{
  // First of all, create the main D3D object. If it is created successfully
  // we should get a pointer to an IDirect3D8 interface.
  g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);

  if(g_pD3D == NULL)
  {
    return E_FAIL;
  }

  //Get the current display mode
  D3DDISPLAYMODE d3ddm;
  if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
  {
    return E_FAIL;
  }

  // Create a structure to hold the settings for our device
  D3DPRESENT_PARAMETERS d3dpp;
  ZeroMemory(&d3dpp, sizeof(d3dpp));

  // Fill the structure: Program shall be windowed,
  // back buffer format matches current display mode
  d3dpp.Windowed         = TRUE;
  d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
  d3dpp.BackBufferFormat = d3ddm.Format;

  //Create a Direct3D device.
  if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
  {
    return E_FAIL;
  }

  return S_OK;
}

void Render()
{
  if(g_pD3DDevice == NULL)
  {
    return;
  }

  // Clear the backbuffer to blue
  g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
                      D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);

  // Begin the scene
  g_pD3DDevice->BeginScene();

  // Fill in here the rendering of other objects

  // End the scene
  g_pD3DDevice->EndScene();

  // Fill back and front buffers so that back buffer will be visible on screen
  g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}

void CleanUp()
{
  if(g_pD3DDevice != NULL)
  {
    g_pD3DDevice->Release();
    g_pD3DDevice = NULL;
  }

  if(g_pD3D != NULL)
  {
    g_pD3D->Release();
    g_pD3D = NULL;
  }
}

void MainLoop()
{
  // Enter the main loop
  MSG  msg;
  BOOL bMessage;

  PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);

  while(msg.message != WM_QUIT)
  {
    bMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);

    if(bMessage)
    {
      // Process message
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
    else
    {
      Render(); // No message to process -> render the scene
    }
  }// while
}

// The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch(msg)
  {
    case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
      break;

    case WM_KEYUP:
      switch (wParam)
      {
        case VK_ESCAPE:
          // Escape key pressed -> exit
          DestroyWindow(hWnd);
          return 0;
          break;
      }
      break;
  }// switch

  return DefWindowProc(hWnd, msg, wParam, lParam);
}

// Application main entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
  //Register the window class
  WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
                    GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                    "DirectX Project", NULL };
  RegisterClassEx(&wc);

  // Create the application's main window
  HWND hWnd = CreateWindow("DirectX Project", "Code::Blocks Template",
                           WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
                           GetDesktopWindow(), NULL, wc.hInstance, NULL);

  // Initialize Direct3D
  if(SUCCEEDED(InitialiseD3D(hWnd)))
  {
    // Show window
    ShowWindow(hWnd, SW_SHOWDEFAULT);
    UpdateWindow(hWnd);

    //Start game running: Enter the game loop
    MainLoop();
  }

  CleanUp();

  UnregisterClass("DirectX Project", wc.hInstance);

  return 0;
}

then when i tried to load it in AutoIt, they didn't load, well, some did, others didn't work. then i tried this:

#include "main.h"

//Define the functions.

AU3_PLUGIN_FUNC g_pFuncs[] =
    {
        //       {"_PointerTest", 0, 0},
        {"Direct3D9DebugConsole", 0 , 0},
        {"_CreateDirect3D9", 3, 4},
        {"BeginSceneRender", 0, 0},
        {"EndSceneRender", 0, 0},
    };

AU3_PLUGINAPI int AU3_GetPluginDetails(int *n_AU3_NumFuncs, AU3_PLUGIN_FUNC **p_AU3_Func)
{
    /* Pass back the number of functions that this DLL supports */
    *n_AU3_NumFuncs = sizeof(g_pFuncs) / sizeof(AU3_PLUGIN_FUNC);

    /* Pack back the address of the global function table */
    *p_AU3_Func = g_pFuncs;

    return AU3_PLUGIN_OK;
}

AU3_PLUGIN_DEFINE(Direct3D9DebugConsole)
{

    AU3_PLUGIN_VAR *pDllResult;
    pDllResult = AU3_AllocVar(); //Allocate Memory for this var.
    HRESULT CoInitEx; //HResult to init COM
    bool Debugger; //Debugger variable
    SHELLEXECUTEINFO pDebuggerInfo; //Get the information for the debugger.


       //Initialize COM
       CoInitEx = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
       if(CoInitEx)
       {
           // HWND pOwner = AU3_GethWnd(pDllResult);

            pDebuggerInfo.cbSize = sizeof(SHELLEXECUTEINFO);
            //pDebuggerInfo.hwnd = pOwner;
            pDebuggerInfo.lpVerb = "open";
            pDebuggerInfo.lpFile = "Direcr3D9 Debug Console.exe";
            pDebuggerInfo.nShow = SW_SHOW;
           // pDebuggerInfo.lpIDList = PCIDLIST_ABSOLUTE;
            Debugger = ShellExecuteEx(&pDebuggerInfo);
            if(Debugger)
            {
                //return A-OK;
                return 1;
            }
            else
            {
                MessageBoxA(0,"Could not load/read the file","Please try again",MB_OK);
                return 0;
            }
            return 1;
       }
       else
       {
           MessageBoxA(0,"Could not initialize COM","Error",MB_OK);
           return 0;
       }
       *p_AU3_Result    = pDllResult;
       *n_AU3_ErrorCode = GetLastError();
       *n_AU3_ExtCode       = 0;

       return AU3_PLUGIN_OK;

}




IDirect3D9* g_pDirect3D9 = NULL;
IDirect3DDevice9* g_pDirect3DDevice9 = NULL;


DLL_EXPORT int CreateDirect3D9(HWND hWnd, UINT width, UINT height)
{
  g_pDirect3D9 = Direct3DCreate9(D3D_SDK_VERSION);

  if(FAILED(g_pDirect3D9))
  {
        OutputDebugStr("Direct3D 9 couldn't be created.");
  }

  D3DPRESENT_PARAMETERS g_pD3DPresentParams;
  ZeroMemory(&g_pD3DPresentParams,sizeof(g_pD3DPresentParams));

  g_pD3DPresentParams.Windowed = true;
  g_pD3DPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
  g_pD3DPresentParams.BackBufferFormat = D3DFMT_UNKNOWN;
  g_pD3DPresentParams.BackBufferWidth = width;
  g_pD3DPresentParams.BackBufferHeight = height;
  g_pD3DPresentParams.hDeviceWindow = hWnd;

  HRESULT D3DDevCreated =  g_pDirect3D9->CreateDevice(
            D3DADAPTER_DEFAULT,
            D3DDEVTYPE_HAL,
            hWnd,
            D3DCREATE_HARDWARE_VERTEXPROCESSING,
            &g_pD3DPresentParams,
            &g_pDirect3DDevice9);

  if(FAILED(D3DDevCreated))
  {

        char Buffer[1000];
            const char* DXErrorString = DXGetErrorStringA(D3DDevCreated);
            const char* DXErrorDesc = DXGetErrorDescriptionA(D3DDevCreated);

            sprintf(Buffer,"Error ceating device: %s : Error Description: %s",DXErrorString,DXErrorDesc);
            MessageBoxA(0, (LPCSTR)Buffer,(LPCSTR)Buffer,MB_OK);

  }
  if(g_pDirect3DDevice9 == NULL)
  {
      return 0;

  }

  return 1;
}

DLL_EXPORT int RenderSceneBegin()
{
    if(g_pDirect3DDevice9)
    {
        if(FAILED(g_pDirect3DDevice9->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 )))
        {
            return 0;
        }
        else
        {
            if(FAILED(g_pDirect3DDevice9->BeginScene()))
            {
                MessageBoxA(0,"Failed to begin scene","Sorry",MB_OK);
            }
            else
            {
                MessageBoxA(0,"D3D Scene active","COOL",MB_OK);
            }
        }
    }

        return 1;
}
DLL_EXPORT int RenderSceneEnd()
{
    g_pDirect3DDevice9->EndScene();

    g_pDirect3DDevice9->Present(NULL,NULL,NULL,NULL);

    return 1;
}

DLL_EXPORT int ShutdownD3D9()
{
    if(g_pDirect3D9 != NULL)
        g_pDirect3D9->Release();

    if(g_pDirect3DDevice9 != NULL)
        g_pDirect3DDevice9->Release();

    return 1;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}

So yea, hopefully someone can put me on the right track, imagine how easily an editor can be made with AutoIt, plus wrapped libs. :-D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...