CFanatic

Go Back   CFanatic > Programming > C# Programming

Join CFanatic Forum Now

Interop: Returning a pointer from C# topic posted under C# Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 09-01-2010, 11:30 PM
 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
daleh@hill-eng.com is on a distinguished road
| More
Interop: Returning a pointer from C#

All,

My question is:
Is it possible to return a pointer allocated in managed code to unmanged code.


I have an unmanged C++ application that calls into a C# dll. The C# dll is build as a CCW and the unmanaged app is build with /clr. I would like to have the unmanaged app be able to get an unmanaged pointer from the C# dll and then read its content. The problem I am running into is that the pointer gets moved so that the unmanged code asserts.

I've seen articles referring to GCHandle but I can't figure out how to call it. Does the managed code call get the handle or the unmanaged code?


A sample of the code is below.

Any help on this would be greatly appreciated.

TIA,
Dale

Code:
// Unmanaged application
int _tmain(int argc, _TCHAR* argv[])
{
      // Get a pointer to the CCW Object
     GBVssNetInterface::_GBVssDeviceComm *pNetInterface;
     CoInitialize(NULL);

     GBVssNetInterface::_GBVssDeviceCommPtr p(__uuidof   (GBVssNetInterface::GBVssDeviceComm));
     pNetInterface = p;

     __int64 pLuns;
     pNetInterface->GetAllVisableLuns(&size, (LONG*)&pLuns);

     // Blows up
     GlobalFree((HGLOBAL)pLuns);
     return 0;
}


// Managed object
namespace GBVssNetInterface
{
    [Guid("D6F88E95-8A27-4ae6-B6DE-0542A0FC7039")]
    [ComVisible(true)] 
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface _GBVssDeviceComm
    {
        [DispId(1)]
        //unsafe int GetAllVisableLuns(int* size, char* pLunBuffer);
        unsafe int GetAllVisableLuns(ref int size, ref Int64 pLunBuffer);
    }
 
    [Guid("4CB08BDA-7F12-458a-AE0B-B6C4E4441822")]
    [ComVisible(true)] 
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("GBVssNetInterface.GBVssDeviceComm")]
    public class GBVssDeviceComm : _GBVssDeviceComm
    {
        public GBVssDeviceComm() {}

        public unsafe int GetAllVisableLuns(ref int Size, ref Int64 pLunBuffer)
        {
            IntPtr lunBuffer = Marshal.AllocHGlobal(size);
            pLunBuffer = lunBuffer.ToInt64();
            return 0;
        }
    }
}
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning object by value or smart pointer mrpringle C++ Programming 0 08-31-2010 04:11 AM
Returning Class from function GameZelda C++ Programming 0 07-05-2010 04:30 AM
Pointer inspiration C++ Programming 10 06-14-2010 07:29 PM
Help on pointer techinspiration C++ Programming 0 05-30-2010 08:55 PM
Write a value-returning function??????? Albaniankool C++ Programming 0 05-30-2007 08:52 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 04:17 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board