On Tue, 6 Mar 2012 07:18:24 -0800 (PST), dev wrote:
> I have rewritten a COM DLL into C#, with a number of interface
> functions, and a number of events that can be fired back to the
> client(s). I can call it from a VB (6) client without problems.
> However, when I try to use a C++ client (still visual studio 6) that
> worked with the old DLL, any call to an interface function gets the
> error "The value of ESP was not properly saved across a function
> call."
Sounds like a calling-convention mismtach.
> [...]
> I can post the full interface if necessary, but it is a big lengthy.
To post an effective question, you need to reproduce the problem with a
smaller example. Start with a single-member COM interface implemented in
C++ and perform the same conversion steps on it to produce the same results
but with a simple example.
> [...]
> I'm wondering if the issue is anything to do with the return type of
> the
> functions. In the old C++ DLL they were defined as STDMETHODIMP
> (i.e. returning an HRESULT). What type should they therefore be in
> C# ?
I haven't memorized the Windows include-file typedefs or the .NET defaults,
but judging from the name and the error, you might have to explicitly
declare your methods as "StdCall".
It's been a couple of years since I've done this, but my recollection is
that VS can import a typelib as a C# interface, providing you with exactly
the right declarations to use. Just start with a COM reference to the old
C++ COM server, and then use that as your starting point for your new C#
COM server. It's a lot easier than hand-porting the original IDL.
Of course, there is the question of whether a fresh rewrite is really the
most practical approach anyway. You can convert the original C++ code to a
mixed-mode C++/CLI DLL, which will allow the COM-facing part of the code to
stay the same (i.e. working :) ), but then also allow the implementation to
reference managed code.
There are still some hoops to jump through to get that to work as well, but
it does allow you to focus on individual parts of the problem at one time.
Pete