The MASM Forum

General => The Workshop => Topic started by: Zen on July 23, 2016, 05:42:27 AM

Title: Marshaling and Attributes
Post by: Zen on July 23, 2016, 05:42:27 AM
Here's a question that's mainly for you theoretical physicists and cosmologists (do I have an awful lot of annoying questions, or what ???):
And, I should preface it by saying that I only have an extremely bleak comprehension of the topic, so, I'm probably asking the wrong question, or phrasing it in the wrong context, or,...typically,...I'm just completely wrong about everything,...:dazzled:

Anyway,...the topic is Marshaling (as in):
Overview of Marshaling in C++, MSDN (https://msdn.microsoft.com/en-us/library/bb384865.aspx)
Interop Marshaling, MSDN (https://msdn.microsoft.com/en-us/library/eaw10et3(v=vs.110).aspx)
Marshaling Data with COM Interop, MSDN (https://msdn.microsoft.com/en-us/library/9f9f3yxf(v=vs.110).aspx)
.NET - COM Interoperability, CodeProject, 2003 (http://www.codeproject.com/Articles/5001/NET-COM-Interoperability)

...And, what I'm interested in is: what is the best method for writing either: a COM Callable Wrapper (https://msdn.microsoft.com/en-us/library/f07c8z1c(v=vs.110).aspx), or, Stub (https://msdn.microsoft.com/en-us/library/windows/desktop/ms694309(v=vs.85).aspx) and Proxy (https://msdn.microsoft.com/en-us/library/windows/desktop/ms686580(v=vs.85).aspx) interoperability Architecture interfaces that will connect and manage elements of native (MASM assembly) code, and either COM or .NET components (or, more likely both).

...And, as a bonus question: How can I reliably implement attributes (which are compiler features, I think),...maybe, as a really complex MACRO (or, hierarchy of MACROS) ??? As examples: IDL Attributes, MSDN (https://msdn.microsoft.com/en-us/library/8tesw2eh.aspx),...and,...COM Attributes, MSDN (https://msdn.microsoft.com/en-us/library/3c3t8ddh.aspx).
Title: Re: Marshaling and Attributes
Post by: rrr314159 on July 23, 2016, 06:54:44 AM
From the cosmology point of view the answer is very simple. Just wait a while. In 5 billion years the sun will become a red giant, Earth will be destroyed, and none of this will matter. You may object: human race will survive on other planets, circling other suns. Well in that case wait a bit longer (100 billion years maybe), Universe is expected to die a "heat death". When there are no molecules left within light years of each other, again, you won't have to worry about this. Perhaps that's too long a time frame? Well, within a mere 100 million years we'll probably be hit by a large meteor, destroying all Windows computers, so that takes care of that problem.

Bottom line, from the theoretical cosmology point of view, Marshaling can simply be ignored. Seems the easiest solution, doesn't it?
Title: Re: Marshaling and Attributes
Post by: hutch-- on July 23, 2016, 07:26:55 AM
 :biggrin:

But will the last computers be running Windows Multidimensional Universe Ultimate  ?
Title: Re: Marshaling and Attributes
Post by: GoneFishing on July 23, 2016, 07:30:14 AM
So only computers with Windows operating system will be destroyed ?
I'm on the safe side  having  Linux on my machine  :biggrin:

Title: Re: Marshaling and Attributes
Post by: rrr314159 on July 23, 2016, 07:41:30 AM
Most theoretical cosmologists don't believe in a benevolent God. But I'm sure they'd all agree that IF such a person exists, then only Windows computers will be destroyed. Linux computers will be preserved in a giant Ark. Unfortunately the very existence of Windows pretty much proves we live in a random, meaningless Universe that doesn't care about us at all. In that case expect the worst: possibly ONLY Windows Multidimensional Universe Ultimate version 10 computers will survive! Linux and Windows 7 pro computers will be right at ground zero when the meteor hits.

Bottom line, given this pessimistic view, what difference does Marshaling really make? Might as well just have a cold beer, watch the tube, wait for death, and vote for Trump.
Title: Re: Marshaling and Attributes
Post by: Zen on July 23, 2016, 07:42:24 AM
WELL,...that's what I get for asking a question like that. :dazzled:
Quote from: GOD...from the theoretical cosmology point of view, Marshaling can simply be ignored,...

...RRR314159 (and, HUTCH),...you guys are hilarious !!! Apparently,...God DOES have a sense of humor,...
...This is kinda disappointing,...though,...from the perspective of my current project...
I can see that indiscriminate, non-denominational, neutrino-based Marshaling is the only rational course of action here,... :dazzled:
Title: Re: Marshaling and Attributes
Post by: rrr314159 on July 23, 2016, 09:09:25 AM
With any luck the best course of action is to wait for someone who knows about marshalling (unlike myself) to provide some useful information :P
Title: Re: Marshaling and Attributes
Post by: jj2007 on July 23, 2016, 12:15:45 PM
Quote from: rrr314159 on July 23, 2016, 06:54:44 AMFrom the cosmology point of view the answer is very simple. Just wait a while. In 5 billion years the sun will become a red giant...

Thank you for putting this into perspective :t I've always wondered why right-clicking on a file takes ages, i.e. Billions of cycles to ask COM for displaying a list of options... it's the cosmology, stupid :icon_redface:
Title: Re: Marshaling and Attributes
Post by: anunitu on July 23, 2016, 08:41:32 PM
Quote from: rrr314159 on July 23, 2016, 07:41:30 AM
Most theoretical cosmologists don't believe in a benevolent God. But I'm sure they'd all agree that IF such a person exists, then only Windows computers will be destroyed. Linux computers will be preserved in a giant Ark. Unfortunately the very existence of Windows pretty much proves we live in a random, meaningless Universe that doesn't care about us at all. In that case expect the worst: possibly ONLY Windows Multidimensional Universe Ultimate version 10 computers will survive! Linux and Windows 7 pro computers will be right at ground zero when the meteor hits.

Bottom line, given this pessimistic view, what difference does Marshaling really make? Might as well just have a cold beer, watch the tube, wait for death, and vote for Trump.


I am liking the cold beer and watch the tube part of your platform,BUT I would use Trump as a first test to stop the giant meteor,if he does not survive,nothing lost..grabs another beer and watches South park as I wait for the end...
Title: Re: Marshaling and Attributes
Post by: Zen on July 24, 2016, 04:33:36 AM
For example: (these are the associated registry keys for the In-Proc server -- mscoree.dll -- System.Runtime.InteropServices namespace)

[.NET Framework Class]   
COM Class CLSID:    {96A058CD-FAF7-386C-85BF-E47F00C81795}
    Value:    [Default] = System.Runtime.InteropServices.InAttribute
SubKey:    Implemented Categories
SubKey:    InprocServer32
    Value:    Class = System.Runtime.InteropServices.InAttribute
    Value:     = mscoree.dll
    Value:    ThreadingModel = Both
    Value:    Assembly = mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    Value:    RuntimeVersion = v1.1.4322
SubKey:    ProgId
    Value:     = System.Runtime.InteropServices.InAttribute


...and,...

[.NET Framework Class]   
COM Class CLSID:    {FDB2DC94-B5A0-3702-AE84-BBFA752ACB36}
    Value:    [Default] = System.Runtime.InteropServices.OutAttribute
SubKey:    Implemented Categories
SubKey:    InprocServer32
    Value:    Assembly = mscorlib, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    Value:    Class = System.Runtime.InteropServices.OutAttribute
    Value:     = mscoree.dll
    Value:    RuntimeVersion = v1.1.4322
    Value:    ThreadingModel = Both
SubKey:    ProgId
    Value:     = System.Runtime.InteropServices.OutAttribute


...And,...yes,...these two interface vtables can be instantiated with either: CoCreateInstance or a COM Class Moniker,...
Title: Re: Marshaling and Attributes
Post by: GoneFishing on July 26, 2016, 02:12:12 AM
See google book : .NET 2.0 Interoperability Recipes: A Problem-Solution Approach by Bruce Bukovics
Chapter 7 describes In / Out attributes
Title: Re: Marshaling and Attributes
Post by: Zen on July 26, 2016, 04:19:13 AM
In the January, 2008 issue of MSDN Magazine there is an article: Marshaling between Managed and Unmanaged Code, by Yi Zhang and Xiaoying Guo, which describes how [InAttribute] and [OutAttribute] control marshaling between unmanaged and managed code. 
Quote from: Yi Zhang and Xiaoying Guo...When applying to method parameters and return values, these attributes control marshaling direction, so they're known as directional attributes. [InAttribute] tells the CLR to marshal data from the caller to the callee at the beginning of the call, while [OutAttribute] tells the CLR to marshal back from the callee to the caller upon return. Both the caller and the callee can be either unmanaged or managed code,...
...These attributes would be fairly easy to implement in MASM assembly,...just by following the conventions of normal .NET marshaling,...
Some of the other .NET attributes, like [PreserveSigAttribute], [StructLayoutAttribute] and [MarshalAs(UnmanagedType], I have no idea how to implement in MASM assembly,...
Here is a listing of: .NET Framework Attributes, MSDN (https://msdn.microsoft.com/en-us/library/system.attribute.aspx)
Here is an article about .NET attributes: Defining and Using Custom Attribute Classes in C#, Code Magazine (http://www.codemag.com/article/0307041)
...And, here is another article:Creating and Using Attributes in Your .NET Application, CodeProject (http://www.codeproject.com/Articles/1811/Creating-and-Using-Attributes-in-your-NET-applicat)
Title: Re: Marshaling and Attributes
Post by: GoneFishing on July 26, 2016, 04:27:28 AM
Quote from: Zen on July 26, 2016, 04:19:13 AM
In the January, 2008 issue of MSDN Magazine there is an article: Marshaling between Managed and Unmanaged Code, by Yi Zhang and Xiaoying Guo, which describes how [InAttribute] and [OutAttribute] control marshaling between unmanaged and managed code. 
...

Got it  here (http://web.archive.org/web/20080128144432/http://msdn.microsoft.com/msdnmag/issues/08/01/CLRInsideOut/default.aspx)

Maybe in  October when I hopefully  get back to programming I'll continue this challenging quest
Title: Re: Marshaling and Attributes
Post by: anunitu on July 26, 2016, 05:18:03 AM
The whole Marshaling thing smacks of the nanny state of matter,and bigger and more intrusive atoms.(yes that was a little humor,very little)
Title: Re: Marshaling and Attributes
Post by: mabdelouahab on July 26, 2016, 09:03:21 AM
Hi Zen

Quote from: Zen on July 26, 2016, 04:19:13 AM
...These attributes would be fairly easy to implement in MASM assembly,...just by following the conventions of normal .NET marshaling,...
Some of the other .NET attributes, like [PreserveSigAttribute], [StructLayoutAttribute] and [MarshalAs(UnmanagedType], I have no idea how to implement in MASM assembly,...
Here is a listing of: .NET Framework Attributes, MSDN (https://msdn.microsoft.com/en-us/library/system.attribute.aspx)
Here is an article about .NET attributes: Defining and Using Custom Attribute Classes in C#, Code Magazine (http://www.codemag.com/article/0307041)
...And, here is another article:Creating and Using Attributes in Your .NET Application, CodeProject (http://www.codeproject.com/Articles/1811/Creating-and-Using-Attributes-in-your-NET-applicat)

It varies depending on the purpose of the use  it
for example:
use it in  managed code when you need to be  specific regarding how your .NET type will be exposed to COM, or when you need to be  specific regarding how COM Class will be import from COM
System.Runtime.InteropServices Attribute Equivalent to COM IDL Def

{Managed:System.Runtime.InteropServices.InAttribute} = {UnManaged:COM IDL parametre attribut [In] }
{Managed:System.Runtime.InteropServices.OUTAttribute}={UnManaged:COM IDL parametre attribut [Out] }

Also you can use System.Runtime.InteropServices Attribute in UnManaged code (like MASM) if you want to create Dynamic Assembly (Define Dynamic Assembly,TypeBuilder , MethodBuilder ,... ) or if you want to create Delegate
Title: Re: Marshaling and Attributes
Post by: Zen on July 27, 2016, 04:55:49 AM
MABDELOUAHAB,
Yeah,...I've been reading up on the various attributes (both COM IDL and .NET), and, in general, the authors describe how the attributes affect the type, or, the operation. I have much to learn, though.
Title: Re: Marshaling and Attributes
Post by: GoneFishing on July 28, 2016, 01:54:23 AM
Zen,
Today I've discovered one valuable VIDEO resource that can help you to understand all INs and OUTs
of Marshaling better :  CLICK ME  ;)  (https://www.youtube.com/channel/UCW7ck0jRwnXqmShjGdsaoPA)
Title: Re: Marshaling and Attributes
Post by: Vortex on July 28, 2016, 02:43:57 AM
About all this unnecessarily complicated subject :

Quote"Any intelligent fool can make things bigger,more complex, and more violent.
It takes a touch of genius -- and a lot of courage --to move in the opposite direction."

E. F. Schumacher

Quote taken from Bogdan Ontanu's website.
Title: Re: Marshaling and Attributes
Post by: jj2007 on July 28, 2016, 03:37:35 AM
Quote from: Vortex on July 28, 2016, 02:43:57 AM
About all this unnecessarily complicated subject

I'm a great admirer of E.F. Schumacher :P
Title: Re: Marshaling and Attributes
Post by: Zen on July 29, 2016, 05:18:38 AM
Quote from: E. F. SCHUMACHER"Any intelligent fool can make things bigger,more complex, and more violent.
It takes a touch of genius -- and a lot of courage --to move in the opposite direction."
...Well,...I'm certainly not a genius,...
Title: Re: Marshaling and Attributes
Post by: Vortex on July 29, 2016, 05:40:24 AM
Quote from: Zen on July 29, 2016, 05:18:38 AM
Quote from: E. F. SCHUMACHERr"Any intelligent fool can make things bigger,more complex, and more violent.
It takes a touch of genius -- and a lot of courage --to move in the opposite direction."
...Well,...I'm certainly not a genius,...

We are on the same boat. No need to be a genius to understand the quote from E. F. Schumacher