Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a problem in pinvoking a vector containing struct to c#

i cannot marshal intptr to string in struct

C++
for (int i = 0; i < itemcount; i++)
{
 var soft = items[i];
string x = Marshal.PtrToStringAuto( soft.DisplayName);
 }



please help

What I have tried:

c++ code
typedef intptr_t ItemListHandle;

bool GetPrograms(InstalledPrograms::ItemListHandle * hItems, Software** itemsFound, int* itemCount)
{
	vector<Software>* lst = InstalledPrograms::GetInstalledPrograms(true);

	*hItems = reinterpret_cast<InstalledPrograms::ItemListHandle>(lst);
	*itemsFound = lst->data();
	*itemCount = lst->size();
	return true;
}
c# code
C#
class ItemsSafeHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        public ItemsSafeHandle()
            : base(true){}
        protected override bool ReleaseHandle()
        {
            return Program.ReleaseItems(handle);
        }
    }
      
    [StructLayout(LayoutKind.Sequential)]
    public struct Software
    {
        public IntPtr DisplayName;
        public IntPtr InstallLocation;
        public IntPtr Version;
        public IntPtr Architecture;
        public IntPtr Icon;
    };
    class Program
    {
        [DllImport(@"mydll.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "GetPrograms",CharSet =CharSet.Auto)]
        static unsafe extern bool GetPrograms(out ItemsSafeHandle itemsHandle, out Software* items, out int itemCount);


        [DllImport(@"mydll.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ReleaseVectorItems", CharSet = CharSet.Auto)]
        public static unsafe extern bool ReleaseItems(IntPtr itemsHandle);
        static unsafe void Main(string[] args)
        {            
            int itemcount = 0;
            Software* items;
            using (GenerateItemsWrapper( out  items, out itemcount))
            {
                for (int i = 0; i <itemcount; i++)
                {
                    var soft = items[i];
                    string x = Marshal.PtrToStringAuto(soft.DisplayName);
                }
            }
        }

        static unsafe ItemsSafeHandle GenerateItemsWrapper(out Software* items, out int itemsCount)
        {
            ItemsSafeHandle itemsHandle;
            if (!GetPrograms(out itemsHandle, out items, out itemsCount))
            {
                throw new InvalidOperationException();
            }
            return itemsHandle;
        }
Posted
Updated 25-Apr-19 20:35pm
v2
Comments
Richard MacCutchan 26-Apr-19 5:34am    
You need to explain exactly what the problem is.
Member 14087451 26-Apr-19 5:47am    
sir, as you can see the software(struct) is having intptr members which iam unable to convert into string values
honey the codewitch 12-Jun-19 8:21am    
can you give us a C/C++ header for the struct you are importing?

I need to see the native types to get you a proper p/invoke

it may need to marshal stringbuilders or fixed length strings inside the struct or something. it could be anything. i need to see the native header for the struct

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900