Story:
I have 2 projects. Project A is a C# project which creates a managed
executable. Project B is a C++ which creates an unmanaged dynamic link
library. Project A is the wrapper/user interface around the legacy
code in project B. Project B contains an application that needs to
communicate through three serial ports. Therefore I created and
exported a function in project B which accepts 3 sets of read and
write function pointers. This function is called from project A with
three sets of read and write delegates which do the reading from and
writing to the serial port.
This works perfectly with simple tests, my function pointers are
assigned in project B and can be accessed without a problem, the call
is received in the managed side. However, if I put a breakpoint at the
real call to the write function in my application and run the
application, the function pointer is empty. The weird thing is, is
that the pointers to the read functions still exist.
Anyone have an idea what the cause could be?
Code:
Below are some copy-pastes from the important bits of code.
-------------------------------------------
Project A - C#
-------------------------------------------
public static SerialPortReadDelegate delTCCRead;
// etc..., notice that the delegates are static!
[DllImport(dllLocation, EntryPoint = "SetCallbacks", SetLastError =
true, CallingConvention = CallingConvention.StdCall)]
public static extern void SetCallbacks(MulticastDelegate
callback1Read, MulticastDelegate callback1Write, MulticastDelegate
callback2Read, MulticastDelegate callback2Write, MulticastDelegate
callback3Read, MulticastDelegate callback3Write);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate void
SerialPortWriteDelegate([MarshalAs(UnmanagedType.LPStr)] string psz);
[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
public delegate char SerialPortReadDelegate();
-------------------------------------------
Project B - C++
-------------------------------------------
// function pointer type defs
typedef void (__stdcall *func_type_write)(char* d); // write
typedef char (__stdcall *func_type_read)(void); // read
// serial port function pointers
static func_type_read serialPort1Read;
static func_type_read serialPort2Read;
static func_type_read serialPort3Read;
static func_type_write serialPort1Write;
static func_type_write serialPort2Write;
static func_type_write serialPort3Write;
// exported function
void SetCallbacks(func_type_read func1Read,
func_type_write func1Write,
func_type_read func2Read,
func_type_write func2Write,
func_type_read func3Read,
func_type_write func3Write)
{
serialPort1Read = func1Read;
serialPort1Write = func1Write;
serialPort2Read = func2Read;
serialPort2Write = func2Write;
serialPort3Read = func3Read;
serialPort3Write = func3Write;
}
#ifdef __cplusplus
extern "C"
{
#endif
DLLexport void SetCallbacks(func_type_read func1Read,
func_type_write func1Write,
func_type_read func2Read,
func_type_write func2Write,
func_type_read func3Read,
func_type_write func3Write);
#ifdef __cplusplus
}
#endif
-------------------------------------------
Jig - 04 Mar 2008 18:18 GMT
Obviously I am talking about Visual Studio projects, both projects are
in one VS2008 solution.