Click here to Skip to main content
15,895,084 members
Home / Discussions / C#
   

C#

 
GeneralProvideProperty for event property Pin
jclanz3-Mar-03 21:10
jclanz3-Mar-03 21:10 
GeneralData Grid doubt Pin
Smitha Nishant3-Mar-03 19:51
protectorSmitha Nishant3-Mar-03 19:51 
GeneralRe: Data Grid doubt Pin
Peter Kiss4-Mar-03 0:34
Peter Kiss4-Mar-03 0:34 
GeneralRe: Data Grid doubt Pin
A.Wegierski4-Mar-03 0:34
A.Wegierski4-Mar-03 0:34 
GeneralRe: Data Grid doubt - Thanks Pin
Smitha Nishant4-Mar-03 4:21
protectorSmitha Nishant4-Mar-03 4:21 
Generalodd form behavior Pin
grv5753-Mar-03 10:51
grv5753-Mar-03 10:51 
Generalscroll and focus bug Pin
bwells3-Mar-03 9:22
bwells3-Mar-03 9:22 
GeneralHelp: Unable To Copy Data (In Structure) from Unmanged MFC/C++ App to .NET Pin
Gaul3-Mar-03 9:00
Gaul3-Mar-03 9:00 
I need to copy and send the data in a private structure from an MFC app to a .NET app. Somehow, the following does not seem to work. When I use the WM_COPYDATA windows message as indicated below, it is not being received by the WndProc on the .NET side, but when I use a private message, it is received. Unfortunately, when I use the private message, the data seems not to be copied across and I get an assertion error for null data. WM_COPY is not received on the .NET side, but when a private message is used, it is received but then Marshal.PtrToStructure asserts reporting a null value.

Using COM interop is not an option. The client thinks that COM will slow things down as the function is called thousands of times.

Does any one have a suggestion, or a better approach?

Thanks

Gaul



****************************
ON THE MFC SIDE
****************************
////////////////////////////////////////////////////////////
// Increment is called on the MFC side
/////////////////////////////////////////Increment Counter
long CTSPerfMon::Increment(int objectID, int instID)
{
_msg.nMsgID = TS_MSG_INCREMENT;
_msg.nObjectID = objectID;
_msg.nInstID = instID;
_msg.nValue = -1;

return SendMessage();
}

// Send Message across to the .NET side module
long CTSPerfMon::SendMessage()
{
// Fill the WIN32 COPYDATASTRUCT structure

_copyData.dwData = _nPerfMsg; // Message identifier
_copyData.cbData = sizeof(_msg); // size of data
_copyData.lpData = &_msg; // data structure

// Call function, passing data in &_msg
long nRetCode = -1;
if ( _hPerfWnd != NULL )
{
HWND hWnd = GetForegroundWindow();
if (hWnd == NULL)
hWnd = GetDesktopWindow();
nRetCode = ::SendMessage( _hPerfWnd,
WM_COPYDATA, //private message works here
(WPARAM)(HWND) hWnd, //windows sending the data
(LPARAM)(LPVOID) &_msg);
}
return nRetCode;
}

****************************
ON THE C# .NET SIDE
****************************

protected override void WndProc(ref System.Windows.Forms.Message m)
{

if (m.Msg == (int) Msgs.WM_COPYDATA)
{

COPYDATASTRUCT copyData = (COPYDATASTRUCT) Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
if ((object) copyData != null)
{
IntPtr nPerfMsg = copyData.dwData;
if (nPerfMsg.ToInt32() == _nPerfMsg)
{
TSMSG msg = (TSMSG) Marshal.PtrToStructure(copyData.lpData, typeof(TSMSG));
int nRetCode = ProcessMessage(ref msg);
m.Result = (IntPtr) nRetCode;
return;
}
}
}
base.WndProc(ref m);

}

Structure definition on the MFC C++ side
----------------------------------------
#ifdef __cplusplus
extern "C" { // Assume C declarations for C++
#endif

typedef struct tagTSMSG
{
int nMsgID;
int nObjectID;
int nValue;
int nInstID;
}TSMSG;

#ifdef __cplusplus
} // End of extern "C" {
#endif // __cplusplus


Structure definitions on the .NET side:
--------------------------------------
[StructLayout(LayoutKind.Sequential)]
public struct TSMSG
{
public int nMsgID;
public int nObjectID;
public int nValue;
public int nInstID;
}

[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}






Gaulles
GeneralSaving Treeview/Listview selections Pin
vlusardi3-Mar-03 7:47
vlusardi3-Mar-03 7:47 
GeneralRegistry and dWord values Pin
codeweenie3-Mar-03 7:36
codeweenie3-Mar-03 7:36 
GeneralRe: Registry and dWord values Pin
leppie3-Mar-03 7:51
leppie3-Mar-03 7:51 
GeneralRe: Registry and dWord values Pin
codeweenie3-Mar-03 8:05
codeweenie3-Mar-03 8:05 
GeneralRe: Registry and dWord values Pin
Paul Riley3-Mar-03 8:31
Paul Riley3-Mar-03 8:31 
GeneralRe: Registry and dWord values Pin
codeweenie3-Mar-03 9:46
codeweenie3-Mar-03 9:46 
GeneralRe: Registry and dWord values Pin
Paul Riley3-Mar-03 11:56
Paul Riley3-Mar-03 11:56 
GeneralRe: Registry and dWord values Pin
leppie3-Mar-03 9:05
leppie3-Mar-03 9:05 
GeneralRe: Registry and dWord values Pin
codeweenie3-Mar-03 9:53
codeweenie3-Mar-03 9:53 
GeneralHelp! Release the COM Pointer Pin
sning3-Mar-03 7:25
sning3-Mar-03 7:25 
GeneralRe: Help! Release the COM Pointer Pin
Jarrod Marshall3-Mar-03 8:11
Jarrod Marshall3-Mar-03 8:11 
GeneralRe: Help! Release the COM Pointer Pin
Stephane Rodriguez.3-Mar-03 8:16
Stephane Rodriguez.3-Mar-03 8:16 
Generalembedding HTML and trapping events.... Pin
LongRange.Shooter3-Mar-03 5:51
LongRange.Shooter3-Mar-03 5:51 
GeneralRe: embedding HTML and trapping events.... Pin
Michael Mac4-Mar-03 9:05
Michael Mac4-Mar-03 9:05 
GeneralThreads and multiple http connections Pin
PeterJensen3-Mar-03 5:05
PeterJensen3-Mar-03 5:05 
GeneralRe: Threads and multiple http connections Pin
leppie3-Mar-03 6:33
leppie3-Mar-03 6:33 
GeneralRe: Threads and multiple http connections Pin
PeterJensen3-Mar-03 7:26
PeterJensen3-Mar-03 7:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.