Click here to Skip to main content
15,881,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to pinvoke
SetupDiGetClassImageIndex

SetupDiGetClassImageIndex function | Microsoft Docs[^]

but it is always returning false, plz help,
what iam doing wrong ?

What I have tried:

[StructLayout(LayoutKind.Sequential)]
       public struct SP_CLASSIMAGELIST_DATA
       {
           public uint cbSize;
           public IntPtr HIMAGELIST;
           public IntPtr Reserved;
       }

[DllImport("Setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        internal static extern bool SetupDiGetClassImageIndex(SP_CLASSIMAGELIST_DATA ClassImageListData, Guid ClassGuid,ref int ImageIndex);


string strGuid="myguid";
SP_CLASSIMAGELIST_DATA m_ImageListData = new SP_CLASSIMAGELIST_DATA();
            bool success = false;
            int ClassImage = 0;
            Guid ClassGuid = new Guid(strGuid);
            
            m_ImageListData.cbSize = (uint)Marshal.SizeOf(typeof(SP_CLASSIMAGELIST_DATA));
            success = SetupDiGetClassImageIndex(m_ImageListData, ClassGuid,ref ClassImage);
Posted
Updated 13-Jul-18 2:25am

1 solution

Call GetLastError() when the function fails. The error code might help finding out what is wrong. I guess it is invalid parameter here.

Note that all parameters of the API function are pointers. So it should be probably (not tested):
C#
[DllImport("Setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        internal static extern bool SetupDiGetClassImageIndex(ref SP_CLASSIMAGELIST_DATA ClassImageListData, Guid ClassGuid, ref int ImageIndex);
The conversion of the C# Guid to the C GUID structure pointer should be AFAIK done by P/Invoke automatically.

Your GUID is hopefully valid because otherwise an exception would be thrown by the Guid() constructor.
 
Share this answer
 
Comments
srilekhamenon 13-Jul-18 8:28am    
getlasterror() function is giving error code 1784, The supplied user buffer is not valid for the requested operation.
Jochen Arndt 13-Jul-18 8:39am    
That refers probably to the PSP_CLASSIMAGELIST_DATA parameter because the function checks the cbSize member.

Did you tried specifying ref (also at the final call)?
srilekhamenon 13-Jul-18 9:00am    
yes sir i had passed SP_CLASSIMAGELIST_DATA as ref but still problem did not solve
Jochen Arndt 13-Jul-18 9:31am    
All I can suggest is trying ref also for the Guid and specifying ImageIndex as out.

Is it a 64-bit or 32-bit application?
Depending on that it might be necessary to add a Pack parameter to the StructLayout (8 for 64 bit and 1 or 4 for 32 bit as used in the setupapi.h file).
srilekhamenon 13-Jul-18 9:39am    
applications target platform is x86 and my system is 64 bit system

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