Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2008

Tip: Looking for answers? Try searching our database.

C# callbacks getting lost in C++

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jig - 04 Mar 2008 15:24 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.