flat assembler
Message board for the users of flat assembler.

Index > Windows > How to create a dll written in c that can be import to fasm?

Author
Thread Post new topic Reply to topic
neworc



Joined: 26 Sep 2023
Posts: 2
neworc 26 Sep 2023, 06:07
I created a dll project by using Dev-c++, code is as below:

dll.h

Code:
#ifndef _DLL_H_
#define _DLL_H_

#include <windows.h>

#if BUILDING_DLL
#define DLLIMPORT __declspec(dllexport)
#else
#define DLLIMPORT __declspec(dllimport)
#endif

DLLIMPORT void HelloWorld();
DLLIMPORT int add(int, int);

#endif

    


dllmain.c

Code:

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


DLLIMPORT void HelloWorld()
{
        MessageBox(0,"Hello World from DLL!\n","Hi",MB_ICONINFORMATION);
}

DLLIMPORT int add(int x, int y)
{
        return x+y;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
        switch(fdwReason)
        {
                case DLL_PROCESS_ATTACH:
                {
                        break;
                }
                case DLL_PROCESS_DETACH:
                {
                        break;
                }
                case DLL_THREAD_ATTACH:
                {
                        break;
                }
                case DLL_THREAD_DETACH:
                {
                        break;
                }
        }
        
        /* Return TRUE on success, FALSE on failure */
        return TRUE;
}

    


libdll1.def
Code:
EXPORTS
    HelloWorld @1
    add @2
    


Then I got dll1.dll.
I can use this dll correctly with c

Code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

typedef int(*lpAdd)(int, int);
typedef void(*lpHello)(void);
HINSTANCE hDll;
lpAdd add;
lpHello hello;

int main()
{
    hDll = LoadLibrary("dll1.dll");
    add = (lpAdd)GetProcAddress(hDll, "add");
    hello = (lpHello)GetProcAddress(hDll, "HelloWorld");
    printf("Hello dll:%d!\n", add(100, 131));
    hello();
    FreeLibrary(hDll);
    return 0;
}
    


But when I tried to import it in fasm, the program wouldn't run.

Code:
section '.idata' import data readable writeable
  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL',\
          dll1, 'DLL1.DLL'
      

  include 'api\kernel32.inc'
  include 'api\user32.inc'
  
  import dll1,\
        hello, 'HelloWorld'
    


What should I do to make it right?
Post 26 Sep 2023, 06:07
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20210
Location: In your JS exploiting you and your system
revolution 26 Sep 2023, 06:37
What does "the program wouldn't run" mean? What error do you see?

Show your fasm code. The snippet given is not enough to diagnose the problem.
Post 26 Sep 2023, 06:37
View user's profile Send private message Visit poster's website Reply with quote
neworc



Joined: 26 Sep 2023
Posts: 2
neworc 26 Sep 2023, 09:16
revolution wrote:
What does "the program wouldn't run" mean? What error do you see?

Show your fasm code. The snippet given is not enough to diagnose the problem.


Thanq buddy, problem solved.
I messed up 32 bit program with 64 bit dll.
Post 26 Sep 2023, 09:16
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.