Friday, November 12, 2010

Against a wall... C#

Today I have realized that my knowledge about C++ and C# is almost like nothing. That counts for JAVA most probably... I've been dealing with different assemblers, shell scripts, perl scripts, a lot of PL/SQL, soma JAVA. And I have worked with ANSI C, C++, C#. And many more.
What is wrong about my skills? I have tried to use DisassembleShader code from the DIrectX. I found that there is ShaderLoader but... no disassembler ?!?
I was able to call the method directly using ImportDll, but then I am not able to work with all the fancy things that are marshaling, reflections, interoperability, unmanaged code, etc.
What I have achieved (maybe someone would find it useful) is:

[DllImport("D3dx9_24.dll" , SetLastError = true)]
// This is probably wrong to use the specific DLL instead of ex. COM GUID
private static unsafe extern IntPtr D3DXDisassembleShader(
[In]void* pShader,
[In]bool EnableColorCode,
[In]string pComments,
[return: MarshalAs(UnmanagedType.Interface)]
[Out]void* ppDisassembly
);

Now the tricky parts.
This method returns HRESULT (where C# equivalent can be IntPtr). The returned value is D3D_OK if everything went OK. D3D_OK maps to S_OK which maps to 0. Thus the expected returned value is 0.
More important piece is the ppDisassembly. This is an UnmanagedType.Interface which is in fact ID3DXBuffer (http://msdn.microsoft.com/en-us/library/bb205753(v=VS.85).aspx). It inherits from IUnknown and adds its own methods there. This is something that I don't know how to access. I have searched over the internet and this knowledge is beyond my current limits.
I think that I should use P/Invoke here, define an interface, access it. But... my knowledge is currently not enough to do this... :(