WinAsm Studio, The Assembly IDE - Free Downloads, Source Code
Forum
   Search    Users    Calendar    Board Help  

Pages: (4) [1] 2 3 ... Last »  ( Go to first unread post ) Reply to this topicStart new topicStart Poll

Declaring and calling exported functions for DLL's, declaring and using exported Dynamic Link Library functions with assembler

Swagler
Posted: Nov 4 2007, 08:14 PM
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 160
Member No.: 4494
Joined: 2-March 07


I'm working on DLL EXPORTS and failing to export variable!
Code snippet is how I do in C
CODE

;In DLL CPP file exported data
#pragma data_seg(".TSKBR")
__declspec( dllexport ) BOOL bInitTskbrCtrl = FALSE;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.TSKBR,RWS")

;In EXE CPP file would import functions/structures/variables
__declspec( dllimport ) BOOL bInitTskbrCtrl;
extern BOOL WINAPI StartTskbrCtrl( HWND hwnd, LPCTSTR szinifile );
extern BOOL WINAPI RestartTskbrCtrl( void );
extern BOOL WINAPI StopTskbrCtrl( void );

Note: above does not require DLL H or DEF files

Code snippet how I'm trying in Masm32
CODE

;DLL Include file
.DATA?
bInitTskbrCtrl BOOL ?

;DLL Definition file
LIBRARY TskbrCtrl.dll
DESCRIPTION 'TskbrCtrl Dynamic Link Library'
SECTIONS .bss READ WRITE SHARED
EXPORTS
   StartTskbrCtrl @1
   StopTskbrCtrl @2
   bInitTskbrCtrl @3 DATA

;EXE ASM file
Extern _imp_bInitTskbrCtrl :PTR BOOL

I have found bits and pieces from all over!
Link below:
DLL Definition file

I would appreciate any help with this!
Would like to have corrected/detailed generic example put in
Snippets/DLL section for others to reference.

PMEmail Poster
Top
samael
Posted: Nov 5 2007, 10:06 AM
Quote Post


Typo-lord
******

Group: Admins
Posts: 289
Member No.: 5187
Joined: 10-June 07


William,

In the attachment you'll find an example of how to declare and use dll exports (both by name and ordinal). Also, you 'll see how you can put a function in a PE segment of your choice, (remember, PE segment names can only be 8 characters long!) as in the code you attached.

Generally speaking, you can directly export data from a dll, but it's a best practice to code a stub that returns a pointer to your data as in the third exported function of this example.
Directly accessing a variable within a DLL, requires knowledge of the Import Address Table - related structures. More on that on the next post...

Attached File ( Number of downloads: 11 )
Attached File  DLL.Example.zip
PM
Top
samael
Posted: Nov 5 2007, 12:12 PM
Quote Post


Typo-lord
******

Group: Admins
Posts: 289
Member No.: 5187
Joined: 10-June 07


And here is the other method to directly access a variable within a dll.
To understand the code, you need to read the following article, and also use the debugger to figure out what happens and why.

Understanding the Import Address Table.

These methods, especially the second and third ones, should work with the vast majority of DLLs produced with MS compilers, but not with all DLLs, because compilers from other vendors may handle imported functions differently (i.e. not via the jmp thunk_address method).

Also, the variable must be exported from the dll, exactly the way an exported function is (by ordinal, or by name).

To summarize: Do not use this method, instead write a stub that exports a pointer for the variable you want to access, as in the example in the above post.
This method is presented here only for the sake of conversation.

Attached File ( Number of downloads: 11 )
Attached File  DIRECTLY.ACCESSING.A.VARIABLE.WITHIN.A.DLL.zip
PM
Top
Swagler
Posted: Nov 6 2007, 11:14 AM
Quote Post


Extremely Active Member
******

Group: Moderators
Posts: 160
Member No.: 4494
Joined: 2-March 07


Nice work samael,

I have been working on this for 5 days, reading alot, hoping
I would'nt have to use Import Address Table. Calling Dll function
to find bool state is not practical for what I'm doing.
Am going to set DLL hook state in INI file for now, until I gain
futher knowledge on this.

I have no issues with functions, I've tested Hook procedure start, stop
and at app exit, all works fine. Dll Exports are being exported, just need
to be able to access variable value. All examples I've seen export a
static/non-changing variables.

I attached a tool to check DLL exports "DLL Export Viewer 1.11".


Attached File ( Number of downloads: 14 )
Attached File  dllexp.zip
PMEmail Poster
Top
samael
Posted: Nov 6 2007, 01:08 PM
Quote Post


Typo-lord
******

Group: Admins
Posts: 289
Member No.: 5187
Joined: 10-June 07


QUOTE
....Calling Dll function to find bool state is not practical for what I'm doing....
....All examples I've seen export a static/non-changing variables...


Was it something like that you had in mind?



Attached File ( Number of downloads: 16 )
Attached File  DLL.Example_3.zip
PM
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

Topic Options Pages: (4) [1] 2 3 ... Last » Reply to this topicStart new topicStart Poll