# [.net] Problem using a dll object object (C# and C++) [SOLVED]

This topic is 3503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Trying to put together a c# wrapper for some DirectX code, nearly there except for one thing that seems to have me stumped. Anytime the class object i create from the dll tries to use an object within its composition it throws a memory exception. The initial Dll object creates ok and even receives and returns values, but a call to any contained objects methods throws the memory error. I have marked the classes for export from the dll. Any ideas would be very welcome. SOLVED Ya i was a silly bugger :p Moral of the story set pointers to null and check before using the bloody things doh :| Also DLL development is horrible especially when it involves three layers :S Win32 C++ DLL -> C++ CLR Wrap -> C# App. My monitor has never felt so small, really could do with 3 :P [Edited by - Guthur on November 18, 2008 3:04:57 PM]

##### Share on other sites
It is just a guess, but might be helpful: DirectX might create resources per process, thus considering the application and the library different processes. I guess that you are actually trying to use a resource created in the library pool and use it in the application pool (at least in .NET there is such a separation between processes, they are called "domain"-s there, I think).

As I said, this is just a guess. I'm a .NET developer, not a DirectX guy.

##### Share on other sites
Sorry i probably should have qualified the early statement its not DirectX interfaces that are causing issues, i can't create my objects to even use the directX components :p

Its strange that it will access the intial dll class but throw exceptions with composition objects :|

First bit of code is the object i want to use, the second is the calling function from the initial dll object. I have tried with out allocating the object on the heap and its still wont work. Its the function that the stack trace ends at but it will run fine as soon as i remove any reference to ShaderCompiler.

#pragma once#include <windows.h>#include <d3d10_1.h>#include <d3dx10.h>#include <string>#ifdef D3D10_GEEK_HELPER_EXPORTS#define D3D10_GEEK_HELPER_API __declspec(dllexport)#else#define D3D10_GEEK_HELPER_API __declspec(dllimport)#endifusing namespace std;class D3D10_GEEK_HELPER_API ShaderCompiler{public:	enum SHADER_TYPE { VERTEX=0, PIXEL=1, GEOMETRY=2 };		ShaderCompiler(void);	~ShaderCompiler(void);		wstring GetDebugString(void);	HRESULT CompileShader ( wstring codeFilepath, unsigned int shader, wstring shaderMain );	ID3D10Blob* m_ShaderBin;	ID3D10Blob* m_CompileOuput;	};

wstring SystemCore::CompileShader( wstring codeFilepath, unsigned int shader, wstring shaderMain ){	wstring debugString;	m_ShaderCompiler = new ShaderCompiler();	m_ShaderCompiler->CompileShader( codeFilepath, 0, shaderMain );	debugString = m_ShaderCompiler->GetDebugString();        delete m_ShaderCompiler; 	return debugString;}

##### Share on other sites
Just realised i probably should have put this in General because though its C# and .NET the problem does actually appear in C++ code :p

##### Share on other sites
I might be really stupid, but I didn't understand again.
1. Where are you actually creating the resource (dll/exe)? Where is it actually used?
2. Are you trying to use a resource (say, some DirectX class, in your case - the thing responsible for shader compilation) that was created in the .dll and then try to access it in the .exe? This might lead to problems (especially memory). It depends very mush on the way DirectX implements resource sharing between threads (well, process domains are different thing, but different processes do use different threads amongst other things).
3. What exactly is the exception? Is it a .NET exception, or a Win32 error code? What is the offending code? Which line exactly?

I'm sorry, but I really can't figure things out without answers to the other questions. Again, I'm a .NET developer, and I have almost no experience in DirectX.

And just a few general hints, not on the topic.

In C++, unlike C#, you write
ClassName *instance = new ClassName;
ClassName *instance = new ClassName();
if you want to use the default constructor.

In an enum, if you are not depending on the actual numeric values, it is much better to declare it as
enum Colours { Colour_White, Colour_Black };
ie - without the "abc=1, cba=2", etc; the compiler will automatically assign them values, beginning at 0 and incrementing by one.

In C++ int is assumed, so you can save typing by using
unsigned shader;
unsigned int shader;

Note:
Fore some reason, the preview hides the plus plus after C; I mean C plus plus, not pure C.

##### Share on other sites
Thanks for the tip on int.

Unfortuately its not really anything you mentioned, the object i was trying to create was basically empty except for a couple of interfaces to shader Blobs and then a call the D3DXCompileFromFile(); I know these are not the issue as i have now moved them to the call function for now and they work, note this is still in the unmanaged dll.

I'm also having terrible issues with my strings here, they all have funny characters, unicode issue i think :S

##### Share on other sites
Well, in my experience with C++ wchar_t is a bit buggy. If you need translation, use gettext for C++ or the integrated xml-based framework in .NET. Don't bother with wstring, unless you are absolutely sure what you are doing.

Hmmm that "moved them to the call function for now and they work" sounds very familiar to me :D. And, by the way, isn't XNA way better for beginners? (compared to DirectX). And as far as I know, there is already a managed wrapper around DirectX - SlimDX. So why bother reinventing the wheel in first place? (I apologize if it sounds offensive, it is now what I intend - I'm being curious, that is all)

##### Share on other sites
hhh its fine, you are understandably thinking that its dx that is the problem but i actually doubt it is. From my searching the only thing i can see that might be it is a security thing with .NET 2.0 and dll's but i can't see how thats the case.

My handling of DirectX isn't two bad usually get things sorted pretty quick there, except maybe the some 3D maths ;)

I want to write my own wrapper because i want a shader development tool for my own directX10 render engine. Plus its good to know these things :).

I have a work around for the strings, sending them wasn't to much of an issue it was getting the shader compile debug info back, so i'm just going to write the debug to a file with fstream and then read it in with StreamReader.

I'm really perplexed by the 'Access Violation Exception Attempted to read or write protected memory' exception. I have been trying everthing to no avail :|. Looks like dll will have tbe just individual classes, i was hoping to hook in my complete engine arch :|

##### Share on other sites
Well, if it is all working it is fine. In the beginning I mentioned "domain"-s. Do search a little big in google if you are interested. In short, the .NET applications all run in the same native process. They have their memory separated by "doimains". The most basic one is the process domain (one process per program/library). Every process has at least one thread, which is commonly named the "main" thread. You can create your own domains within your process' domain, but it is very rare and I have never had to do it, nor felt the need to.

##### Share on other sites
dilyan_rusev thanks any suggestion is welcome, and indeed yours sounds as plausible as any other.

I have come to the conclusion i don't like coding wrappers very much :| especially when its only to faciliate other more exciting projects :p I'm going to make a dll with just 2 classes and see if that works, tried there but there was no lib file for some reason :s i'll look into that now.

Problem is i'd rather be working on the HLSL IDE or render engine than concocting some sort of glue between them :|.

It actually gave me a thought last night of stripping some classes down futher, taking functional and data abstraction a little futher maybe.

As things stand anyway the compiler really does not need to be included with the engine code, it can be just a standalone singleton, it needs nothing more than the file name and some flags to do its thing. Which will mean this issue wont be an issue anymore :p

Edit
Putting the render engine in might be an issue though :(

#2 I have seen a lot of information regarding issues with STL in dll's exposed to managed code. Something to do with STL statics.

I'm not going to get bogged down in this issue but if i find the reason i'll post.

[Edited by - Guthur on November 18, 2008 10:47:32 AM]

• 11
• 19
• 12
• 9
• 34
• ### Forum Statistics

• Total Topics
631398
• Total Posts
2999840
×