flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Using an assembly DLL with C-Sharp.

Author
Thread Post new topic Reply to topic
madmatt



Joined: 07 Oct 2003
Posts: 1046
Location: Michigan, USA
madmatt
I've just figured out how to use an assembly dll in C-Sharp, and example project is included below, along with the dll file that you can put either in your system32 folder, or in the Debug/Release folder along with the executable (Sorry for not releaseing the source, Later On I'll make a simple dll and use that in this example). I made this in Visual C-Sharp Express 2005 (the freebie from Microsoft). So you CAN use assembly in C-Sharp!!! I'm one very happy assembly programmer now Very Happy , (I was before thanks to Tomaszs' FasmW, but now the glow is much brighter now! Razz Laughing ).


Description: Using Assembly DLL with C-Sharp project:
Download
Filename: LearningProject1.rar
Filesize: 35.9 KB
Downloaded: 564 Time(s)

Post 13 Dec 2006, 22:59
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7106
Location: Slovakia
vid
i have read article from MS saying you can even mix C# procedures and asm procedures in single DLL Exclamation
Post 13 Dec 2006, 23:19
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
in visual studio (i only tested this with C++) you can use certain assembly code in masm format by using the keyword __asm (and yes, that's 2 underscores) for one liners and __asm { code } for multi-liners. Perhaps you could try that, as well.
Post 14 Dec 2006, 01:55
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1046
Location: Michigan, USA
madmatt
vid, kohlrak: You can use x86 assembly directly in your C++ code but NOT in C-Sharp code, you have to use a dll like my example above to use x86 assembly. x86 assembly was left out to make C-Sharp machine independent, but with apple moving to intel this is a moot point now. Almost all Desktop/Laptop OS's use intel/amd x86 processors now.
Post 14 Dec 2006, 09:01
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7106
Location: Slovakia
vid
madmatt: i didn't say you can. what i was mentioning is called "mixed DLL" by MS. Search it on MSDN.
Post 14 Dec 2006, 12:56
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1046
Location: Michigan, USA
madmatt
Here's pretty much the same example, but now shows how to import a windows api function and now uses the assembly dll found in the fasmw16x/examples/dll.

vid: Sorry, didn't mean to imply that you were wrong. But I read somewhere that you couldn't code x86 assembly directly into C-Sharp code, only in C++. How would you mix the two without creating A separate x86 assembly dll? Do you know of an example that demonstrates what you are talking about?


Description: New C-Sharp Example of import x86 Assembly and raw win32 api function(s).
Download
Filename: LearningProject1.rar
Filesize: 18.9 KB
Downloaded: 482 Time(s)

Post 14 Dec 2006, 18:27
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7106
Location: Slovakia
vid
i just have read about that.

something is mentioned here: http://msdn2.microsoft.com/en-gb/library/aa290048(VS.71).aspx
Post 14 Dec 2006, 19:36
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 444
Location: BC, Canada
RedGhost
All you have to do is use DllImport

[DllImport("dll", ... extra)]
public static extern <type> <name> ( ... );

Sometimes you must flag your compiler to undecorate the exported stdcall functions. You can also define the type of string (unicode or ansi) and the convention, which defaults to stdcall.

I use ufmod in my C# loaders this way.

madmatt, C# also supports delegates (pointer to function), you can make a byte buffer of opcodes, append a retn and make a delegate to this and call it for "ghetto inline asm" in C#. Here is an example: http://www.atrevido.net/blog/PermaLink.aspx?guid=ac03f447-d487-45a6-8119-dc4fa1e932e1

_________________
redghost.ca
Post 14 Dec 2006, 23:34
View user's profile Send private message AIM Address MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7106
Location: Slovakia
vid
it would be funny to have example of mixed dll
Post 14 Dec 2006, 23:59
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1046
Location: Michigan, USA
madmatt
Redghost: The example you posted reminds me of using data statements on my old commodore 64 computer to "POKE" (store) an assembly program. Smile (Damn! I hated typing in all that raw data Shocked ). You probably have no idea what a Commdore 64 was, so enough of that. While the example you posted will work, it will get complicated (and buggy) fast when you start using complex instructions, algorithms and gui functions. Because fasmw supports making compatible dll's, fasmw makes the whole process of using asm in managed code a Exclamation LOT Exclamation easier! (as my example shows).

vid: I think I see what you mean Idea . I guess it is possible if you use Visual C++ .NET and the _asm keyword. I wonder if the _asm keyword works in C++ .NET Question . When I had Visual C++ installed, I never used C++ .NET, just standard C++.
Post 15 Dec 2006, 00:20
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7106
Location: Slovakia
vid
madmatt: i think such DLLs can be only created by linking "native" (x86) object with .net object. But that is just quessing
Post 15 Dec 2006, 13:44
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Filter



Joined: 08 Oct 2006
Posts: 67
Filter
vid wrote:
madmatt: i think such DLLs can be only created by linking "native" (x86) object with .net object. But that is just quessing


Actually (at least in .net 2.0) you can have hybrid native/managed dll. It doesn't matter if you have inline asm in the native parts of the dll.

BTW: an example of a hybrid dll would be the SQLite ADO .NET 2.0 provider...
http://sqlite.phxsoftware.com/ ... it doesn't contain assembler to my knowledge just native code.

It can actually be used in place of the native code sqlite dll if necessary or just used as an ADO .NET 2.0 provider.
Post 03 Feb 2007, 16:34
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-2019, Tomasz Grysztar.

Powered by rwasa.