Click here to Skip to main content
15,885,869 members
Home / Discussions / C#
   

C#

 
AnswerRe: Microsoft.Office.Interop.Excel.Application issue Pin
MumbleB20-May-09 21:40
MumbleB20-May-09 21:40 
GeneralRe: Microsoft.Office.Interop.Excel.Application issue Pin
AndieDu21-May-09 0:09
AndieDu21-May-09 0:09 
AnswerRe: Microsoft.Office.Interop.Excel.Application issue Pin
AmmySA20-May-09 22:38
AmmySA20-May-09 22:38 
GeneralRe: Microsoft.Office.Interop.Excel.Application issue Pin
AndieDu21-May-09 0:08
AndieDu21-May-09 0:08 
AnswerRe: Microsoft.Office.Interop.Excel.Application issue Pin
AndieDu21-May-09 17:28
AndieDu21-May-09 17:28 
AnswerRe: Microsoft.Office.Interop.Excel.Application issue Pin
AmmySA22-May-09 5:39
AmmySA22-May-09 5:39 
GeneralRe: Microsoft.Office.Interop.Excel.Application issue Pin
AndieDu22-May-09 16:14
AndieDu22-May-09 16:14 
QuestionC# calling unmanaged C/C++ COM method with C++ reference type Pin
Klempie20-May-09 20:44
Klempie20-May-09 20:44 
Hi there. Hope this is in the right place since I *think* it's language specific.

I am currently creating an interop DirectX library and providing methods and structs on the C# side as needed. I'm translating one of the DirectX tutorial samples to test. Things were going swimmingly with all of the methods returning the correct data and setting relevant pointers etc, until I got to this line of code:

result = d3d10Engine.DXGISwapChain.GetBuffer(0, ref gs, out pBackBuffer);

At this point a SystemAccessViolation exception is thrown with message details "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

The GetBuffer method is declared as:

[PreserveSig]<br />
int GetBuffer(uint Buffer, ref TestConfigurationManager.GuidStruct refiid, [Out] out IntPtr ppSurface);


where TestConfigurationManager.GuidStruct is defined as:

[StructLayout(LayoutKind.Sequential)]<br />
public struct GuidStruct<br />
{<br />
    public uint Data1;<br />
    public ushort Data2;<br />
    public ushort Data3;<br />
    public byte b0;<br />
    public byte b1;<br />
    public byte b2;<br />
    public byte b3;<br />
    public byte b4;<br />
    public byte b5;<br />
    public byte b6;<br />
    public byte b7;<br />
}


The DirectX API defines the method signature as:

HRESULT GetBuffer(<br />
  UINT Buffer,<br />
  REFIID riid,<br />
  void **ppSurface<br />
);


However, once the C++ preprocessor has done its bit, it actually looks like this:

long GetBuffer(<br />
  uint Buffer,<br />
  const GUID & riid,<br />
  void **ppSurface<br />
);


where GUID is defined as:

typedef struct _GUID {<br />
    unsigned long  Data1;<br />
    unsigned short Data2;<br />
    unsigned short Data3;<br />
    unsigned char  Data4[ 8 ];<br />
} GUID;


Originally I had Data1 as ulong in the GuidStruct and then realised that ulong and unsigned long in C++ have different sizes and that could be causing the access violation by overrunning the stack allocation during marshaling but I've changed it to uint now and I still got the error. I thought it could perhaps be the void pointer parameter but IntPtr should cover that fine as it's just an integer address surely? So the last theory I have is that it has something to do with the fact that the unmanaged method receives a C++ reference and not a pointer. Trawling through the documentation on MSDN regarding marshaling it seems to me that if you're using the ref argument, the marshal copies a pointer onto the unmanaged stack to the memory space of the guid in the CLR. However, because the unmanaged side expects a reference, it doesn't deference the pointer and when it accesses what it thinks is a reference, an access violation results.

Is this the case and is there any way to deal with it, or would I have to resort to C++/CLI?

Thanks in advance.
AnswerRe: C# calling unmanaged C/C++ COM method with C++ reference type Pin
Klempie21-May-09 2:54
Klempie21-May-09 2:54 
GeneralRe: C# calling unmanaged C/C++ COM method with C++ reference type Pin
Klempie21-May-09 3:50
Klempie21-May-09 3:50 
QuestionSystem font scripts Pin
fungweizz20-May-09 20:38
fungweizz20-May-09 20:38 
AnswerRe: System font scripts Pin
Baran M20-May-09 21:00
Baran M20-May-09 21:00 
GeneralRe: System font scripts Pin
fungweizz20-May-09 21:55
fungweizz20-May-09 21:55 
QuestionVery urgent please Pin
Naveed72720-May-09 20:28
Naveed72720-May-09 20:28 
AnswerRe: Very urgent please Pin
Mycroft Holmes20-May-09 21:22
professionalMycroft Holmes20-May-09 21:22 
GeneralRe: Very urgent please Pin
Naveed72720-May-09 21:30
Naveed72720-May-09 21:30 
GeneralRe: Very urgent please Pin
Mycroft Holmes20-May-09 21:45
professionalMycroft Holmes20-May-09 21:45 
AnswerRe: Very urgent please Pin
MumbleB20-May-09 21:48
MumbleB20-May-09 21:48 
GeneralRe: Very urgent please Pin
Naveed72721-May-09 0:08
Naveed72721-May-09 0:08 
QuestionHow to pass values to managed to unmanaged code? Pin
mutpan20-May-09 19:31
mutpan20-May-09 19:31 
AnswerRe: How to pass values to managed to unmanaged code? Pin
Klempie20-May-09 20:50
Klempie20-May-09 20:50 
GeneralRe: How to pass values to managed to unmanaged code? Pin
mutpan20-May-09 21:58
mutpan20-May-09 21:58 
GeneralRe: How to pass values to managed to unmanaged code? Pin
Klempie21-May-09 0:33
Klempie21-May-09 0:33 
Questionpointer and address? Pin
dec8220-May-09 19:10
dec8220-May-09 19:10 
AnswerRe: pointer and address? Pin
Samer Aburabie20-May-09 20:49
Samer Aburabie20-May-09 20:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.