The MASM Forum

General => The Campus => Topic started by: Zen on July 13, 2013, 03:40:36 AM

Title: Really Bizarre Question
Post by: Zen on July 13, 2013, 03:40:36 AM
Warning: This is an actual question,...not a ridiculous joke.

I stumbled upon this article over at CodeProject: Test TCP/IP Connectivity Through aspx Page (http://www.codeproject.com/Tips/618603/Test-TCP-IP-Connectivity-Through-aspx-Page),...which seemed like a pretty cool idea.
And, so,...I was wondering,...can you write an automated program in assembly that would generate ASP.NET web pages ???
...I'm initially assuming that it is possible, but,...so horrendously error-prone and time-consuming that no one actually does it that way.

Anyone here ever done any ASP.NET (or, even, NET Framework) programming ????
Title: Re: Really Bizarre Question
Post by: Zen on July 14, 2013, 03:10:07 AM
I remember about a year ago,...someone was asking if you could use a NET Framework member function from within an assembly language program. No one seemed to know the answer. I have programmed with the NET Framework,...but, it's silly,...you are basically calling an interface that then calls the underlying Windows API that assembly programers normally invoke from an assembly program.
Title: Re: Really Bizarre Question
Post by: jj2007 on July 14, 2013, 04:03:02 AM
What exactly do you mean with "an automated program in assembly that would generate ASP.NET web pages"?
Title: Re: Really Bizarre Question
Post by: hutch-- on July 14, 2013, 01:41:12 PM
It sounds like a variant of conventional CGI. Instead of creating ordinary HTML, it would be a generator of .NET style pages.
Title: Re: Really Bizarre Question
Post by: Zen on July 16, 2013, 03:23:43 AM
JOCHEN,   
Quote from: JOCHENWhat exactly do you mean with "an automated program in assembly that would generate ASP.NET web pages"?
Yeah,...excellent question. I hadn't really thought it all the way through. But, I'm assuming that to invoke NET Framework objects and their member functions from an assembly program, you would have to have a version of the NET Framework library residing on your computer,...and, then, you'd have to write some kind of header or include file that would prototype these routines correctly. My initial concept was to use Dumpbin (http://msdn.microsoft.com/en-us/library/c1h23y6c(v=vs.71).aspx) to determine the names of the exported NET interfaces, and then, LoadLibrary (http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175(v=vs.85).aspx), and GetProcAddress (http://msdn.microsoft.com/en-us/library/windows/desktop/ms683212(v=vs.85).aspx) to return an address, that could be used with the assembler call syntax.
At that point, I was just going to wing it,...
...It was actually more of a hypothetical question than anything else. The main concept was to see if a NET Framework member function could actually be called from a compiled assembly language program. My operating assumption is that it is possible, but, would be so inconvenient that everyone would simply use a NET Framework compiler.
I read somewhere, years ago, that NET Framework objects are actually implemented as COM interfaces,...so, the invoke or call technique would be somewhat similar.
Title: Re: Really Bizarre Question
Post by: jj2007 on July 16, 2013, 03:30:48 AM
Ok, so you want to mimic functionality of the page, not generate the page itself (the latter sounded too simple to be true).

Sounds difficult. This is badly documented stuff...
Title: Re: Really Bizarre Question
Post by: Zen on July 16, 2013, 03:51:22 AM
When the NET Framework was first released (I think 2002), I purchased a book by the title of: .NET and COM: The Complete Interoperability Guide, by Adam Nathan. Incredible book, but the syntax for COM interop from NET Framework executables was very tedious and unpleasant.
I was thinking that the NET Framework member functions could be used in much the same way as JAPHETH (http://www.japheth.de/) does with his COM interfaces COM and Assembly (http://www.japheth.de/COMnASM.html), essentially by addressing the COM Virtual Table, using simple addressing.
Here is a blog post from Raymond Chen: The Layout of a COM Object (http://blogs.msdn.com/b/oldnewthing/archive/2004/02/05/68017.aspx)

Quote from: JJOCHENSounds difficult. This is badly documented stuff...
...Yeah,...I don't think it's documented anywhere,... :dazzled:
Title: Re: Really Bizarre Question
Post by: GoneFishing on July 16, 2013, 05:53:37 AM
Hi,
I have  very basic understanding of Net framework but I  think that working with NET through ASM is  very sophisticated task.
If we look into the import section of NET executables we will see the only entry:
_CorExeMain  or _CorDllMain (mscoree.dll)
Here (http://msdn.microsoft.com/en-us/library/k5532s8a.aspx#running_code) is a small portion of info on how  CLR is initialised and managed module is executed. It may be useful to explore mscoree.tlb (from Microsoft.Net\framework\x.x...) in Visual Studio Object Browser and mscoree.h /mscoree.idl files (from Windows SDK includes) in editor .
NET  debuggers and ILDASM are good tools to dig into NET internals.

P.S. : About runtime hosting (http://msdn.microsoft.com/en-us/library/dd380850.aspx)

HTH




Title: Re: Really Bizarre Question
Post by: Zen on July 17, 2013, 03:27:03 AM
Clearly, I need to do alot more research.
Anyway, here is a link to the MSDN section: Developing Web Applications with ASP.NET (http://msdn.microsoft.com/en-us/library/bb400852.aspx)
From the introduction: ASP.NET Web Pages is a framework that you can use to create dynamic web pages. And, you need a computer that is running Windows 7, Windows Vista SP2, Windows XP SP3, Windows Server 2003 SP2, Windows Server 2008, or Windows Server 2008 R2. They recommend that you should be familiar with both HTML and CSS.

EDIT: Added July 17,...I started a thread over at CodeProject in the NET Framework Forum and asked if: a NET Framework interface can be Invoked from MASM Assembly Language (http://www.codeproject.com/Messages/4613592/Invoke-from-MASM-Assembly-Language.aspx). (Derek Tortonian is one of Baltoro's many aliases.)
MSDN states: Unmanaged applications can also benefit from hosting the common language runtime. (A MASM Assembly Language Program would be considered an unmamaged application.)
Also, MSDN provides this overview of: Hosting Overview (for .NET Framework version 4.5) (http://msdn.microsoft.com/en-us/library/dd380850.aspx)

Finally,...here's an article from CodeProject on: Injecting .Net Assemblies Into Unmanaged Processes (http://www.codeproject.com/Articles/607352/Injecting-Net-Assemblies-Into-Unmanaged-Processes)
Title: Re: Really Bizarre Question
Post by: GoneFishing on July 18, 2013, 06:12:32 PM
...
Title: Re: Really Bizarre Question
Post by: GoneFishing on July 18, 2013, 07:18:01 PM
...
Title: Re: Really Bizarre Question
Post by: Zen on July 19, 2013, 04:01:11 AM
VERTOGRAD,
Quote from: VERTOGRADThe time has come to code MASM version of this tricky stuff.  ;)

You are right,...unfortunately, my computer died about two years ago.
...Think of it as a "Classic Thought Experiment",... :icon_eek:
Title: Re: Really Bizarre Question
Post by: GoneFishing on July 19, 2013, 05:08:33 PM
Oh, I see ...

Are there any chances  to repair your computer or to buy a new (or used) one not-too-expensive?
Anyway keep feeding your brain with MASM.
I'll  post some code here soon  ;)

                   
Title: Re: Really Bizarre Question
Post by: GoneFishing on August 16, 2013, 11:27:02 PM
Finally I did it ...
Here's a very small application which enumerates installed .NET Framework runtimes.
It uses hosting API to interact with CLR.

NB: It requires  .NET Framework 4  to run / build it! .
You may run the following line* in the command prompt to check what runtimes are present on your computer:
dir  /b  /ad  c:\windows\Microsoft.NET\Framework\v*
*assuming that c:\ is your system root
or
check the version of mscoree.dll in system32 directory.

You will need mscoree.lib(it's not included in masm32 package) to build it ( don't forget to change the path in the includelib section)

P.S. more coming soon ...
Title: Re: Really Bizarre Question
Post by: Zen on August 17, 2013, 03:39:39 AM
VERTOGRAD,
Very impressive initial attempt. I quickly scanned your code; it's simple and elegant.
...I even ran the app,...but, I am on a public terminal (public library in California, running Windows XP),...and, I got the error message (which I expected).
It is entirely possible that you are the first programmer in the galaxy to actually compile a MASM app that correctly queries the installed NET Framework version (if any).
Title: Re: Really Bizarre Question
Post by: GoneFishing on August 17, 2013, 05:36:56 AM
Zen,

First of all, I'd like to thank you for your encouraging words!
Now I want to show you some more results of my work ...

you wrote:
QuoteI was thinking that the NET Framework member functions could be used in much the same way as JAPHETH does with his COM interfaces COM and Assembly, essentially by addressing the COM Virtual Table, using simple addressing.

Here's the sequence of calls for obtaining the pointer to virtual functions table of _AppDomain interface which
exposes the public members of the System.AppDomain class to unmanaged code. (http://msdn.microsoft.com/en-us/library/system._appdomain.aspx)
   invoke CLRCreateInstance,addr CLSID_CLRMetaHost,addr IID_ICLRMetaHost,addr  ppvICLRMetaHost
    ...
    ICLRMetaHost GetRuntime,pszVersion,addr IID_ICLRRuntimeInfo,addr  ppvICLRRuntimeInfo
    ...
    ICLRRuntimeInfo IsLoadable,addr fLoadable
    ...
    ICLRRuntimeInfo GetInterface, addr CLSID_CorRuntimeHost,addr IID_CorRuntimeHost , addr ppvICorRuntimeHost     
    ...
    ICorRuntimeHost Start
    ...
    ICorRuntimeHost GetDefaultDomain,addr  ppvIUnknown       
    ...
    IUnknown QueryInterface,addr IID_AppDomain ,addr  ppvIAppDomain



So now  we have a pointer to IAppDomain and we can use any of its functions( see dump of virtual functions table below ( to dump it yourself you should uncomment lines 39 and 80 of RuntimeHostV4Demo1.ASM , change MAKEIT.BAT to include symbolic info into the executable and  rebuild all)).
WinDbg's command window line:
dt -b  AsmHostCLR!vftAppDomain
and its output:
+0x000 QueryInterface   : (null)
   +0x004 AddRef           : (null)
   +0x008 Release          : (null)
   +0x00c GetTypeInfoCount : (null)
   +0x010 GetTypeInfo      : (null)
   +0x014 GetIDsOfNames    : (null)
   +0x018 renamed_Invoke   : (null)
   +0x01c get_ToString     : (null)
   +0x020 Equals           : (null)
   +0x024 GetHashCode      : (null)
   +0x028 GetType          : (null)
   +0x02c InitializeLifetimeService : (null)
   +0x030 GetLifetimeService : (null)
   +0x034 get_Evidence     : (null)
   +0x038 add_DomainUnload : (null)
   +0x03c remove_DomainUnload : (null)
   +0x040 add_AssemblyLoad : (null)
   +0x044 remove_AssemblyLoad : (null)
   +0x048 add_ProcessExit  : (null)
   +0x04c remove_ProcessExit : (null)
   +0x050 add_TypeResolve  : (null)
   +0x054 remove_TypeResolve : (null)
   +0x058 add_ResourceResolve : (null)
   +0x05c remove_ResourceResolve : (null)
   +0x060 add_AssemblyResolve : (null)
   +0x064 remove_AssemblyResolve : (null)
   +0x068 add_UnhandledException : (null)
   +0x06c remove_UnhandledException : (null)
   +0x070 DefineDynamicAssembly : (null)
   +0x074 DefineDynamicAssembly_2 : (null)
   +0x078 DefineDynamicAssembly_3 : (null)
   +0x07c DefineDynamicAssembly_4 : (null)
   +0x080 DefineDynamicAssembly_5 : (null)
   +0x084 DefineDynamicAssembly_6 : (null)
   +0x088 DefineDynamicAssembly_7 : (null)
   +0x08c DefineDynamicAssembly_8 : (null)
   +0x090 DefineDynamicAssembly_9 : (null)
   +0x094 CreateInstance   : (null)
   +0x098 CreateInstanceFrom : (null)
   +0x09c CreateInstance_2 : (null)
   +0x0a0 CreateInstanceFrom_2 : (null)
   +0x0a4 CreateInstance_3 : (null)
   +0x0a8 CreateInstanceFrom_3 : (null)
   +0x0ac Load             : (null)
   +0x0b0 Load_2           : (null)
   +0x0b4 Load_3           : (null)
   +0x0b8 Load_4           : (null)
   +0x0bc Load_5           : (null)
   +0x0c0 Load_6           : (null)
   +0x0c4 Load_7           : (null)
   +0x0c8 ExecuteAssembly  : (null)
   +0x0cc ExecuteAssembly_2 : (null)
   +0x0d0 ExecuteAssembly_3 : (null)
   +0x0d4 get_FriendlyName : (null)
   +0x0d8 get_BaseDirectory : (null)
   +0x0dc get_RelativeSearchPath : (null)
   +0x0e0 get_ShadowCopyFiles : (null)
   +0x0e4 GetAssemblies    : (null)
   +0x0e8 AppendPrivatePath : (null)
   +0x0ec ClearPrivatePath : (null)
   +0x0f0 SetShadowCopyPath : (null)
   +0x0f4 ClearShadowCopyPath : (null)
   +0x0f8 SetCachePath     : (null)
   +0x0fc SetData          : (null)
   +0x100 GetData          : (null)
   +0x104 SetAppDomainPolicy : (null)
   +0x108 SetThreadPrincipal : (null)
   +0x10c SetPrincipalPolicy : (null)
   +0x110 DoCallBack       : (null)
   +0x114 get_DynamicDirectory : (null)


Also many of the mscorelib classes exposed to unmanaged code can be used through IDispatch interface methods .Though I didn't try it yet .

Now a few words about the attachment.
I tried to translate to MASM this CPP project :C++ app hosts CLR 4 and invokes .NET assembly (CppHostCLR) (http://code.msdn.microsoft.com/windowsdesktop/CppHostCLR-e6581ee0)
I used headinc utility (by ToutEnMasm) to partly translate metahost.h to include format. Also I use his <Interface> macro.
I've preserved almost all the CPP comments and included the original Documentation  folder into the attachment .
Once again:
NB: It requires  .NET Framework 4  to run / build it! .
It was tested under Windows 7 32 bit only.

Zen,
Thank you for the  good idea. I've been working on it almost all this month. In fact, sometimes it took me 2-3 days to figure out what's wrong in a single line of code .

Have fun!

P.S. I also like libraries   :t

EDIT: added AsmHostCLRv2  for .NET Framework  2.0.50727.
It contains only two demo out of three in AsmHostCLR  for .NET Framework 4.0.30319 because in-process side-by-side hosting  (http://msdn.microsoft.com/en-us/library/ee518876.aspx)is not supported in v2.




EDIT: attachments are temporary removed

Title: Re: Really Bizarre Question
Post by: dedndave on August 18, 2013, 01:23:07 AM
good job !   :t

we have all wondered about writing .NET code
i guess we were too lazy to figure it out - lol

i am running XP Media Center Edition 2005
it is not compatible with .NET 4.0
well - i can disable certain MCE features and make .NET 4.0 work ok
but - i haven't had the time to figure out if i can switch on the fly
so, my current build only has .NET 3.5 and below
Title: Re: Really Bizarre Question
Post by: GoneFishing on August 18, 2013, 01:25:52 AM
Thanks , Dave !

Try AsmHostCLRv2 . It runs on .NET 2.0
Title: Re: Really Bizarre Question
Post by: dedndave on August 18, 2013, 01:27:27 AM
yes - that one works fine   :t
Title: Re: Really Bizarre Question
Post by: qWord on August 18, 2013, 04:28:06 AM
nice, even if it is in its infancy  :t
Title: Re: Really Bizarre Question
Post by: Zen on August 21, 2013, 05:56:13 AM
VERTOGRAD,   
It's amazing that you were able to create an assembly language project that is able to determine whether the NET Framework is installed on the user's computer.
I've had a number of thoughts in the aftermath, and, I was wondering if you could clarify.

...And, finally,...do you think we should have a HUGE, WILD PARTY,...to celebrate your accomplishment ???   :dazzled:
Title: Re: Really Bizarre Question
Post by: GoneFishing on August 21, 2013, 09:38:43 PM
Zen,

I'll try to answer all your questions:
(http://imageshack.us/a/img202/8697/rcle.png) (http://imageshack.us/photo/my-images/202/rcle.png/)

Recently I've read online a book of Steven Pratschner "Customizing the Microsoft® .NET Framework Common Language Runtime".
From Foreword:
QuoteIn October 1999, someone I knew at Microsoft asked me if I would do some consulting work on the Microsoft .NET Framework team. At that time, I knew very little about the .NET Framework, but what I did know impressed me quite a bit, and I immediately joined the team.

At that time, the .NET Framework was internally called COM+ 2.0. But Microsoft knew that this new way of programming deserved a better name than that. Before its first release, the .NET Framework had many other names. One code name was "Lightning."
Did you notice an icon of ILDASM on the picture?   ;)


Quote...And, finally,...do you think we should have a HUGE, WILD PARTY,...to celebrate your accomplishment ???

That sounds good ... but I'm afraid I know nothing about parties    (except one called "A Mad Tea Party" (http://google.com/))

I'm currently working on ASP.NET part of your initial question. 
It may take me some time to post an example of hosting ASP.NET runtime .

Title: Re: Really Bizarre Question
Post by: dedndave on August 21, 2013, 09:47:24 PM
it should work

(http://masm32.com/board/index.php?action=dlattach;topic=2106.0;attach=2051)
Title: Re: Really Bizarre Question
Post by: GoneFishing on August 21, 2013, 10:12:45 PM
Later I'll figure out what I was doing wrong    ;)
I'll think about it tomorrow  :biggrin:

Dave:
        How did you manage to get a picture of me?  :biggrin:

EDIT: That's it
Title: Re: Really Bizarre Question
Post by: Paulo on August 22, 2013, 12:04:05 AM
 :lol:

(http://s9.postimg.org/ar53jtenz/Before_Long.jpg)
Title: Re: Really Bizarre Question
Post by: dedndave on August 22, 2013, 05:16:59 AM
i hope that's a $1.00 bill - can't afford $100   :lol:
i also hope she plans on washing that towel when she's done
Title: Re: Really Bizarre Question
Post by: Zen on August 22, 2013, 05:34:13 AM
VERTOGRAD, 
Hey, thanks for taking the time to answer those questions. So, it really is an unmanaged code application.   
Seeing as how you have accomplished the near-impossible,...I have alot more complex stuff bouncing around in my mind,...
BUT,...I'm NOT going to ask, because, you are fearless,...you will try to code a demo,...and, we'll never hear from you again !!!  :bgrin:

By the way, AsmHostCLRv2  for .NET Framework  2.0.50727 works correctly on the public terminal in a public library (somewhere in California).

You've opened the door, though,...we know it's possible (even if it is insanely complex),...and, other assembly programmers will go where no assembly programmer has gone before.
Title: Re: Really Bizarre Question
Post by: Farabi on August 22, 2013, 11:45:15 PM
Quote from: dedndave on August 22, 2013, 05:16:59 AM
i hope that's a $1.00 bill - can't afford $100   :lol:
i also hope she plans on washing that towel when she's done

Ahem, dave. That is mean I will stick to my new wife for a long time. :greensml:
Title: Re: Really Bizarre Question
Post by: Paulo on August 23, 2013, 12:44:44 AM
Quote from: dedndave on August 22, 2013, 05:16:59 AM
i also hope she plans on washing that towel when she's done

Judging by the look on her face, I suspect there was a lot of flux residue on the towel.  :biggrin:

Apologies to all for hijacking the thread.
Title: Re: Really Bizarre Question
Post by: Zen on August 23, 2013, 03:25:37 AM
PAULO,   
Quote from: PAULOApologies to all for hijacking the thread.
No problem,...we all hijack DAVE's threads all the time.  :icon_redface: