none
Memory access (or pointer size) problem when marshalling a win32 method on a 32-bit system RRS feed

  • Question

  •   Hi,

    I am working on Dtc Transactions in C#, and I'm trying to use a win32 method IGetDispenser::GetDispenser. This method can replace ITransactionDispenser::QueryInterface. (To get ITransactionDispenser requires loading a xolehlp.dll file and marshalling DtcGetTransactionManager, and I would like to avoid using another dll in the project). On a 32-bit system, the last line of the following section of code gives "AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." But it is fine on a 64-bit system.

    CommittableTransaction tr = new CommitableTransaction();  
    ITransaction itr = (ITransaction)TransactionInterop.GetDtcTransaction(tr); //Marshalled ITransaction   
    IntPtr p1 = Marshal.GetIUnknownForObject(itr);  
    IntPtr p2;  
    int hr = Marshal.QueryInterface(p1, IID_IGetDispenser, out p2); // This line is fine  
    IGetDispenser idispenser = (IGetDispenser) Marshal.GetObjectForIUnknown(p2); //Marshalled IGetDispenser  
    idispenser.GetDispenser(IID_IDtcLuConfigure, out p2); //This line gives the error 


    Is there any way to get around, for example by replacing IntPtr by something else, or by getting ITransactionDispenser from some other object(not using dll)? I have tried using AllocHGlobal to allocate some memory for p2 or replacing IntPtr by Int64 but the situation does not change.

    Any suggestion?
    Thanks,
    Joyce
    Monday, December 29, 2008 3:40 PM

All replies

  • Hello, Joyce,

    IntPtr is 4 bytes on 32-bit systems, so I suspect the problem may be in the IGetDispenser.GetDispenser implementation.

    I'm not a COM interop person, but I'd check that p2 != IntPtr.Zero, too.

           -Steve
    Tuesday, December 30, 2008 2:31 PM
  • Thanks Steve.

    I have turned back to C++ for my project and everything is fine. I know little about interop but it seems that the marshalling process is quite suspicious.

    Joyce
    Wednesday, December 31, 2008 5:43 AM