mfc >> About Structures

by Christopher J. Holland.............................................................................. » Wed, 02 Feb 2005 05:51:19 GMT

Hi,

I am a having a bit of trouble with a structure.

I just declared the structure the same as the way as it is shown.
typedef struct tagSCROLLBARINFO {
DWORD cbSize;
RECT rcScrollBar;
int dxyLineButton;
int xyThumbTop;
int xyThumbBottom;
int reserved;
DWORD rgstate[CCHILDREN_SCROLLBAR+1];
} SCROLLBARINFO, *PSCROLLBARINFO, *LPSCROLLBARINFO;

Normally, I would do something like this...
SCROLLBARINFO ScrollInfo;
ScrollInfo.cbSize = sizeof(SCROLLBARINFO);
m_sbHorz.GetScrollBarInfo(&ScrollInfo);
It complains "cannot convert parameter 1 from 'SCROLLBARINFO *' to
'PSCROLLBARINFO' "
I tried to cast it as a PSCROLLBAR by doing
m_sbHorz.GetScrollBarInfo((PSCROLLBARINFO)&ScrollInfo);

I tried this....
PSCROLLBARINFO pScrollInfo;
pScrollInfo->cbSize = sizeof(SCROLLBARINFO);
m_sbHorz.GetScrollBarInfo(pScrollInfo);
It complains "cannot convert parameter 1 from 'PSCROLLBARINFO' to
'PSCROLLBARINFO' "
which doesn't really make sense.

I tried a bunch of other things to no avail.

I am all confused. How do I get it to work?
and why do they put "*PSCROLLBARINFO, *LPSCROLLBARINFO"
at the end of the structure declaration?


Thanks,
--
Christopher J. Holland [!MVP]
http://www.mvps.org/vcfaq/
http://www.codeguru.com
http://www.codeproject.com
http://www.naughter.com/
http://support.microsoft.com/default.aspx
http://msdn.microsoft.com/howto/
http://msdn.microsoft.com/library/
www.flounder.com/mvp_tips.htm




mfc >> About Structures

by Scott McPhillips [MVP] » Wed, 02 Feb 2005 07:32:52 GMT


Christopher J.
Holland...........................................................................................................




I think you have two different declarations of SCROLLBARINFO here, each
with a different scope. Microsoft's declaration is in windows.h with
global scope. Then you have your own declaration, perhaps with some
local scope. The compiler has no idea that they are the same (or are
supposed to be the same). Get rid of your declaration! The struct is
already defined for you as part of the API.

--
Scott McPhillips [VC++ MVP]




Similar Threads

1. Returning a C# structure containing a nested array of structures

2. Interop Structure with array of Structures

I have an interop question:

I'm trying to get the ICONDIR Win32 struct which contains an array of 
ICONDIRENTRY structures to marshall correctly. The problem is that the array 
isn't a set amount and could be any number of ICONDIRENTY structures. The 
good news is that the ICONDIR structure does include the number of entries 
in the ICONDIRENTRY array. How do I properly marshal the structure so that I 
can properly recieve all the structures in the array?


Thanks,
Jacob 


3. C Structure to C# structure using sockets - CSharp/C#

4. C++ structure to C# structure - help needed

I have converted a few C++ structures to C# structures. However, when I use 
them in the code, I get errors in  "internal static BluetoothDeviceInfo 
Create()". I feel I'm doing something wrong while converting the union. 
Could someone help please?

/*
typedef ULONGLONG BTH_ADDR;
typedef struct _BLUETOOTH_ADDRESS {
    union {
        BTH_ADDR ullLong; // easier to compare again BLUETOOTH_NULL_ADDRESS
        BYTE rgBytes[ 6 ];    // easier to format when broken out
    };
} BLUETOOTH_ADDRESS_STRUCT;
#define BLUETOOTH_ADDRESS BLUETOOTH_ADDRESS_STRUCT
#define BLUETOOTH_NULL_ADDRESS ( (ULONGLONG) 0x0 )
*/
[StructLayout(LayoutKind.Explicit)]
internal class BluetoothAddress
{
    [MarshalAs(UnmanagedType.U8)]
    [FieldOffset(0)] internal ulong ullLong;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
    [FieldOffset(0)] internal byte[] rgBytes;

    internal BluetoothAddress()
    {
        ullLong = 0;
        rgBytes = new byte[6];
    }
}


/*
typedef struct _BLUETOOTH_DEVICE_INFO {
    DWORD dwSize; // size, in bytes, of this structure - must be the 
sizeof(BLUETOOTH_DEVICE_INFO)
    BLUETOOTH_ADDRESS Address; // Bluetooth address
    ULONG ulClassofDevice; // Bluetooth "Class of Device"
    BOOL fConnected; // Device connected/in use
    BOOL fRemembered; // Device remembered
    BOOL fAuthenticated; // Device authenticated/paired/bonded
    SYSTEMTIME stLastSeen; // Last time the device was seen
    SYSTEMTIME stLastUsed; // Last time the device was used for other than 
RNR, inquiry, or SDP
    WCHAR szName[ BLUETOOTH_MAX_NAME_SIZE ]; // Name of the device
} BLUETOOTH_DEVICE_INFO_STRUCT;
#define BLUETOOTH_DEVICE_INFO BLUETOOTH_DEVICE_INFO_STRUCT
typedef BLUETOOTH_DEVICE_INFO * PBLUETOOTH_DEVICE_INFO;
*/
internal struct SystemTime
{
    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;
}

[StructLayout(LayoutKind.Sequential)]
internal struct BluetoothDeviceInfo
{
    [MarshalAs(UnmanagedType.U4)]
    internal int dwSize;
    [MarshalAs(UnmanagedType.Struct)]
    internal BluetoothAddress Address;
    internal ulong ulClassOfDevice;
    internal bool fConnected;
    internal bool fRemembered;
    internal bool fAuthenticated;
    internal SystemTime stLastSeen;
    internal SystemTime stLastUsed;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=32)]
    internal char[] szName;

    internal static BluetoothDeviceInfo Create()
    {
        BluetoothDeviceInfo bdi = new BluetoothDeviceInfo();
        //Throws an error on the next line
        //"Type System.Net.Bluetooth.BluetoothDeviceInfo can not be 
marshaled as an unmanaged structure; no meaningful size or offset can be 
computed."
        bdi.dwSize = Marshal.SizeOf(typeof(BluetoothDeviceInfo));
        bdi.szName = new char[32];
        return bdi;
    }
}


/*
typedef struct _BLUETOOTH_DEVICE_SEARCH_PARAMS {
    DWORD dwSize; // IN sizeof this structure
    BOOL fReturnAuthenticated; // IN return authenticated devices
    BOOL fReturnRemembered; // IN return remembered devices
    BOOL fReturnUnknown; // IN return unknown devices
    BOOL fReturnConnected; // IN return connected devices
    BOOL fIssueInquiry; // IN issue a new inquiry
    UCHAR cTimeoutMultiplier; // IN timeout for the inquiry
    HANDLE hRadio; // IN handle to radio to enumerate - NULL == all radios 
will be searched
} BLUETOOTH_DEVICE_SEARCH_PARAMS;
*/
[StructLayout(LayoutKind.Sequential)]
internal struct BluetoothDeviceSearchParams
{
    [MarshalAs(UnmanagedType.U4)]
    internal int dwSize;
    internal bool fReturnAuthenticated;
    internal bool fReturnRemembered;
    internal bool fReturnUnknown;
    internal bool fReturnConnected;
    internal bool fIssueInquiry;
    internal ushort cTimeoutMultiplier;
    internal IntPtr hRadio;

    internal static BluetoothDeviceSearchParams Create(IntPtr hRadio)
    {
        BluetoothDeviceSearchParams dsp = new BluetoothDeviceSearchParams();
        dsp.dwSize = Marshal.SizeOf(typeof(BluetoothDeviceSearchParams));
        dsp.hRadio = hRadio;
        return dsp;
    }
}

Thanks & Regards,

B Vidyadhar Joshi. 


5. Structure array inside structure when passing reference to C++ - CSharp/C#

6. Passing structure pointer within structures

Hi,
I am new to C programming. Here is what I am trying to do

typedef struct A {
	char *a;
}A;

struct B {
	int b;
	A *asample;
};


void main()
{
	struct B bk;
}

Now I want to allocate Hello to bk->asample->a. How do I proceed here. Any 
help will be appreciated

Thanks,
Kunal

7. Copying unmanaged memory pointer of structure to managed structure array

8. convert C++ structure to Matlab structure

Is there any guidance available ? Thx