Author Topic: Writing DLL with NASM  (Read 17571 times)

Offline Dibya

  • Jr. Member
  • *
  • Posts: 19
Writing DLL with NASM
« on: August 22, 2017, 04:05:27 PM »
Hi
I want to compile a dll with following code
EXPORT ERRORINVESTIGATOR (Let take it as Export function )

Code ::
push 57h
call RtlSetLastWin32Error
xor     al, al
leave
retn    0Ch

Any one can teach can i compile simple dll with this one using NASM

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Writing DLL with NASM
« Reply #1 on: August 26, 2017, 07:13:19 AM »
Hi Dibya,

I can assure you that Nasm can assemble your dll. Unfortunately, I can't tell you how - I don't do Windows. Unless I'm mistaken, it's mostly a linker function. Read the manual for your linker, if you haven't.

I think you may have to add to your code:

Code: [Select]
extern RtlSetLastWin32Error

push 57h
call RtlSetLastWin32Error
xor     al, al
leave
retn    0Ch

You might get some information from:

https://forum.nasm.us/index.php?topic=2302.0

As you will see there, Nasm has two output formats that can be used for Windows. One is "-f obj" - it is, by default, a 16-bit format, but there's a 32-bit extension to it. It is a PITA and rather obsolete. It has the advantage that it has "import" and "export". The more modern output format is "-f win32" (and "-f win64" if that's what you need) It does not have "import" or "export"... but I'm certain there's a way to do it.

Hopefully, a Windows user will come along and help you more...

Best,
Frank


Offline Dibya

  • Jr. Member
  • *
  • Posts: 19
Re: Writing DLL with NASM
« Reply #2 on: August 27, 2017, 07:15:41 AM »
-f win32 i Want to use as -fobj i can make dll easily but code is obsolete. 
How can i use following import -fwin32?
extern RtlSetLastWin32Error
import RtlSetLastWin32Error ntdll.dll
EXTERN NtQueryInformationFile
IMPORT NtQueryInformationFile ntdll.dll
EXTERN RtlRaiseException
IMPORT RtlRaiseException ntdll.dll
EXTERN lstrcatW
IMPORT lstrcatW kernel32.dll
EXTERN lstrlenW
IMPORT lstrlenW kernel32.dll
EXTERN lstrcpyW
IMPORT lstrcpyW kernel32.dll
EXTERN LoadLibraryA
IMPORT LoadLibraryA kernel32.dll
EXTERN GetProcAddress
IMPORT GetProcAddress kernel32.dll
EXTERN SHParseDisplayName
IMPORT SHParseDisplayName shell32.dll
EXTERN SHCreateShellItem
IMPORT SHCreateShellItem shell32.dll
EXTERN ILFree
IMPORT ILFree shell32.dll
EXTERN SHGetFolderPathW
IMPORT SHGetFolderPathW shell32.dll
More over how to export ?

Offline Dibya

  • Jr. Member
  • *
  • Posts: 19
Re: Writing DLL with NASM
« Reply #3 on: August 27, 2017, 07:18:16 AM »
How can i use link.exe to build dll ? I am bored with alink .

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Writing DLL with NASM
« Reply #4 on: August 27, 2017, 04:06:02 PM »
Ask Micro$oft.

Best.
Frank


Offline Dibya

  • Jr. Member
  • *
  • Posts: 19
Re: Writing DLL with NASM
« Reply #5 on: August 28, 2017, 04:38:29 PM »
Lol Someone help me with import and export . I can use GoLink even..

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Writing DLL with NASM
« Reply #6 on: August 28, 2017, 07:51:22 PM »
It occurs to me that the "nasmx" package has macros for import and export. I ASSume that they're for use with "-f win32". At a quick glance, I don't see quite what they do. "nasmx" is quite complex - macros define or enable other macros... By the time I find the part that "does the work", I've forgotten what I wanted to know. We can delve into that, if you think it'll help...

With GoLink, you can list the .dlls where the functions are found in the command line, and GoLink will find 'em without resorting to "import". So I understand. I'm less sure what "export" does, beyond what "global" does...

Have you tried GoLink at all? If so, what happens?

If I understand the situation, your code might assemble and link alright, but when you try to use your .dll, the lack of "export" might make itself known. Does that seem right?

I wish I could help you more, but I'm a stubborn old geezer and I'm not going back to Windows. We have the source to Alink, so I can compile it to run on Linux. I can't run the output from it, but at least I can see the error messages. We don't even have that option with GoLink. I know we have Windows users. I hope one will happen along soon...

Best,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Writing DLL with NASM
« Reply #7 on: August 29, 2017, 02:36:39 AM »
I ASSume that if you download GoLink, you get the manual. In case that isn't true:

http://www.godevtool.com/GolinkHelp/GoLink.htm

Looking through the NASMX files, it looks to me that the "import" macro is not the same as the "import" directive in "-f obj". They seem to use "IMPORT" on everything in sight, and it does not seem to specify the .dll where it can be found, but the number of parameters(?). I think I was mistaken about "export", which I think is what you want, at present. Both terms do appear in the GoLink manual. All I can suggest is "try it and see what happens".

Best,
Frank


Offline Dibya

  • Jr. Member
  • *
  • Posts: 19
Re: Writing DLL with NASM
« Reply #8 on: August 29, 2017, 03:47:33 PM »
I ASSume that if you download GoLink, you get the manual. In case that isn't true:

http://www.godevtool.com/GolinkHelp/GoLink.htm

Looking through the NASMX files, it looks to me that the "import" macro is not the same as the "import" directive in "-f obj". They seem to use "IMPORT" on everything in sight, and it does not seem to specify the .dll where it can be found, but the number of parameters(?). I think I was mistaken about "export", which I think is what you want, at present. Both terms do appear in the GoLink manual. All I can suggest is "try it and see what happens".

Best,
Frank
I read it before but it is confusing for me .
I wish some one can explain me .
Frankly speaking i am a newbie regarding all this linker ,

Offline Dibya

  • Jr. Member
  • *
  • Posts: 19
Re: Writing DLL with NASM
« Reply #9 on: August 29, 2017, 03:51:54 PM »
how can i install this macro?
https://github.com/nalsakas/pe

Offline Dibya

  • Jr. Member
  • *
  • Posts: 19
Re: Writing DLL with NASM
« Reply #10 on: August 29, 2017, 03:56:17 PM »
I got it .
this is a way to go
https://github.com/nalsakas/pe