HighTechTalks DotNet Forums  

Unmanaged Code to CCW/C# problem

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss Unmanaged Code to CCW/C# problem in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
GlennAnthonyB
 
Posts: n/a

Default Unmanaged Code to CCW/C# problem - 08-18-2007 , 08:18 AM






Hi

I have a C# DLL which has a method which accepts a string argument. This is
registered for COM. The resulting .tlb is then referenced in a native C++
DLL. I then wrap the method in a C++ method and add some trace code. The
wrapped C# method also includes some trace code. When I attempt to run the
C++ method, passing a BSTR to it, the C++ methods trace code fires, however,
the C# method call doesn't appear to do anything.

The strange thing is that if I remove the string argument from the C#
method, regenerate the .tlb and change the wrapper method accordingly the C#
trace code fires. I have other methods which accept and return numeric
types which issue, it's just this one which accepts a BSTR that seems to be
a problem.

I should say that the C++ dll has to be native to integrate with an ancient
PowerBuilder component which can't be changed, hence the C++ to COM to C#
process.

I'm rather lost on this one, as I know almost nothing about C++, so I'd
really appreciate if anyone can give me a few pointers (no pun intended).

Many Thanks

Glenn



Reply With Quote
  #2  
Old   
Johannes Passing
 
Posts: n/a

Default Re: Unmanaged Code to CCW/C# problem - 08-18-2007 , 09:51 AM






GlennAnthonyB wrote:
Quote:
Hi

I have a C# DLL which has a method which accepts a string argument. This is
registered for COM. The resulting .tlb is then referenced in a native C++
DLL. I then wrap the method in a C++ method and add some trace code. The
wrapped C# method also includes some trace code. When I attempt to run the
C++ method, passing a BSTR to it, the C++ methods trace code fires, however,
the C# method call doesn't appear to do anything.
Does anything mean that the call succeeds though the trace code is not
executed or does it hang?

Quote:
The strange thing is that if I remove the string argument from the C#
method, regenerate the .tlb and change the wrapper method accordingly the C#
trace code fires. I have other methods which accept and return numeric
types which issue, it's just this one which accepts a BSTR that seems to be
a problem.

I should say that the C++ dll has to be native to integrate with an ancient
PowerBuilder component which can't be changed, hence the C++ to COM to C#
process.
Can you please post the affected code of both the C# and the C++ side?

--Johannes


--
Johannes Passing - http://int3.de/


Reply With Quote
  #3  
Old   
glennanthonyb
 
Posts: n/a

Default Re: Unmanaged Code to CCW/C# problem - 08-20-2007 , 04:15 AM




"Johannes Passing" <jpassing_at_hotmail_com (AT) nospam (DOT) com> wrote

Quote:
GlennAnthonyB wrote:
Hi

I have a C# DLL which has a method which accepts a string argument. This
is registered for COM. The resulting .tlb is then referenced in a native
C++ DLL. I then wrap the method in a C++ method and add some trace code.
The wrapped C# method also includes some trace code. When I attempt to
run the C++ method, passing a BSTR to it, the C++ methods trace code
fires, however, the C# method call doesn't appear to do anything.
Does anything mean that the call succeeds though the trace code is not
executed or does it hang?
It's doesn't hang, it just doesn't appear to enter the C# method.

Quote:
The strange thing is that if I remove the string argument from the C#
method, regenerate the .tlb and change the wrapper method accordingly the
C# trace code fires. I have other methods which accept and return
numeric types which issue, it's just this one which accepts a BSTR that
seems to be a problem.

I should say that the C++ dll has to be native to integrate with an
ancient PowerBuilder component which can't be changed, hence the C++ to
COM to C# process.
Can you please post the affected code of both the C# and the C++ side?
C# code fragment...

public int Search( string searchString )
{
Trace.TraceInformation( "Managed Search method invoked..." );

int status = -1;

return status;
}

C++ code fragment...

#include "stdafx.h"

#import "Test.tlb" raw_interfaces_only

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
CoInitialize( NULL );
return TRUE;
}

int Search( BSTR searchString )
{
long lResult = 0;

Test::ISearchEnginePtr searchEngine( __uuidof( Test::SearchEngine ) );

searchEngine->Search( searchString, &lResult );

printf( "result %d", lResult );

return lResult;
}


Quote:
--Johannes


--
Johannes Passing - http://int3.de/
Thanks

Glenn




Reply With Quote
  #4  
Old   
glennanthonyb
 
Posts: n/a

Default Re: Unmanaged Code to CCW/C# problem - 08-20-2007 , 06:06 AM




Interesting point is that if I create a BSTR in the C++ method I can pass
that to the COM wrapped C# method and it works. The issue is using the BSTR
argument passed to the C++ method.


"glennanthonyb" <glenn.csharp (AT) yahoo (DOT) co.uk> wrote

Quote:
"Johannes Passing" <jpassing_at_hotmail_com (AT) nospam (DOT) com> wrote in message
news:O4Vzu7Z4HHA.5880 (AT) TK2MSFTNGP03 (DOT) phx.gbl...
GlennAnthonyB wrote:
Hi

I have a C# DLL which has a method which accepts a string argument.
This is registered for COM. The resulting .tlb is then referenced in a
native C++ DLL. I then wrap the method in a C++ method and add some
trace code. The wrapped C# method also includes some trace code. When I
attempt to run the C++ method, passing a BSTR to it, the C++ methods
trace code fires, however, the C# method call doesn't appear to do
anything.
Does anything mean that the call succeeds though the trace code is not
executed or does it hang?

It's doesn't hang, it just doesn't appear to enter the C# method.


The strange thing is that if I remove the string argument from the C#
method, regenerate the .tlb and change the wrapper method accordingly
the C# trace code fires. I have other methods which accept and return
numeric types which issue, it's just this one which accepts a BSTR that
seems to be a problem.

I should say that the C++ dll has to be native to integrate with an
ancient PowerBuilder component which can't be changed, hence the C++ to
COM to C# process.
Can you please post the affected code of both the C# and the C++ side?

C# code fragment...

public int Search( string searchString )
{
Trace.TraceInformation( "Managed Search method invoked..." );

int status = -1;

return status;
}

C++ code fragment...

#include "stdafx.h"

#import "Test.tlb" raw_interfaces_only

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
CoInitialize( NULL );
return TRUE;
}

int Search( BSTR searchString )
{
long lResult = 0;

Test::ISearchEnginePtr searchEngine( __uuidof( Test::SearchEngine ) );

searchEngine->Search( searchString, &lResult );

printf( "result %d", lResult );

return lResult;
}



--Johannes


--
Johannes Passing - http://int3.de/

Thanks

Glenn




Reply With Quote
  #5  
Old   
Johannes Passing
 
Posts: n/a

Default Re: Unmanaged Code to CCW/C# problem - 08-20-2007 , 11:52 AM



Hi Glenn,

first, there are some issues with your DllMain. DllMain is not only
called on initialization but also on termination of process/threads, so
you definitely need to switch on ul_reason_for_call rather than always
perform the same action. Furthermore, you should never call CoInitialize
from DllMain - see [1] for details. Depending on the calling
application, this might even be the reason for your problem, so try to
fix this first.

To use COM in a DLL, create a helper thread (in the C++ Search function)
and perform all COM-related work on this thread.

[1] http://download.microsoft.com/download/a/f/7/
af7777e5-7dcd-4800-8a0a-b18336565f5b/DLL_bestprac.doc

--Johannes

Quote:

"glennanthonyb" <glenn.csharp (AT) yahoo (DOT) co.uk> wrote in message
news:eBWg9Iw4HHA.5160 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
"Johannes Passing" <jpassing_at_hotmail_com (AT) nospam (DOT) com> wrote in message
news:O4Vzu7Z4HHA.5880 (AT) TK2MSFTNGP03 (DOT) phx.gbl...
GlennAnthonyB wrote:
Hi

I have a C# DLL which has a method which accepts a string argument.
This is registered for COM. The resulting .tlb is then referenced in a
native C++ DLL. I then wrap the method in a C++ method and add some
trace code. The wrapped C# method also includes some trace code. When I
attempt to run the C++ method, passing a BSTR to it, the C++ methods
trace code fires, however, the C# method call doesn't appear to do
anything.
Does anything mean that the call succeeds though the trace code is not
executed or does it hang?
It's doesn't hang, it just doesn't appear to enter the C# method.

The strange thing is that if I remove the string argument from the C#
method, regenerate the .tlb and change the wrapper method accordingly
the C# trace code fires. I have other methods which accept and return
numeric types which issue, it's just this one which accepts a BSTR that
seems to be a problem.

I should say that the C++ dll has to be native to integrate with an
ancient PowerBuilder component which can't be changed, hence the C++ to
COM to C# process.
Can you please post the affected code of both the C# and the C++ side?
C# code fragment...

public int Search( string searchString )
{
Trace.TraceInformation( "Managed Search method invoked..." );

int status = -1;

return status;
}

C++ code fragment...

#include "stdafx.h"

#import "Test.tlb" raw_interfaces_only

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
CoInitialize( NULL );
return TRUE;
}

int Search( BSTR searchString )
{
long lResult = 0;

Test::ISearchEnginePtr searchEngine( __uuidof( Test::SearchEngine ) );

searchEngine->Search( searchString, &lResult );

printf( "result %d", lResult );

return lResult;
}


--Johannes


--
Johannes Passing - http://int3.de/
Thanks

Glenn




--
Johannes Passing - http://int3.de/


Reply With Quote
  #6  
Old   
glennanthonyb
 
Posts: n/a

Default Re: Unmanaged Code to CCW/C# problem - 08-21-2007 , 08:25 AM



Thanks for the info, appreciate it.

Glenn

"Johannes Passing" <jpassing_at_hotmail_com (AT) nospam (DOT) com> wrote

Quote:
Hi Glenn,

first, there are some issues with your DllMain. DllMain is not only called
on initialization but also on termination of process/threads, so you
definitely need to switch on ul_reason_for_call rather than always perform
the same action. Furthermore, you should never call CoInitialize from
DllMain - see [1] for details. Depending on the calling application, this
might even be the reason for your problem, so try to fix this first.

To use COM in a DLL, create a helper thread (in the C++ Search function)
and perform all COM-related work on this thread.

[1] http://download.microsoft.com/download/a/f/7/
af7777e5-7dcd-4800-8a0a-b18336565f5b/DLL_bestprac.doc

--Johannes



"glennanthonyb" <glenn.csharp (AT) yahoo (DOT) co.uk> wrote in message
news:eBWg9Iw4HHA.5160 (AT) TK2MSFTNGP05 (DOT) phx.gbl...
"Johannes Passing" <jpassing_at_hotmail_com (AT) nospam (DOT) com> wrote in message
news:O4Vzu7Z4HHA.5880 (AT) TK2MSFTNGP03 (DOT) phx.gbl...
GlennAnthonyB wrote:
Hi

I have a C# DLL which has a method which accepts a string argument.
This is registered for COM. The resulting .tlb is then referenced in
a native C++ DLL. I then wrap the method in a C++ method and add some
trace code. The wrapped C# method also includes some trace code. When
I attempt to run the C++ method, passing a BSTR to it, the C++ methods
trace code fires, however, the C# method call doesn't appear to do
anything.
Does anything mean that the call succeeds though the trace code is not
executed or does it hang?
It's doesn't hang, it just doesn't appear to enter the C# method.

The strange thing is that if I remove the string argument from the C#
method, regenerate the .tlb and change the wrapper method accordingly
the C# trace code fires. I have other methods which accept and return
numeric types which issue, it's just this one which accepts a BSTR
that seems to be a problem.

I should say that the C++ dll has to be native to integrate with an
ancient PowerBuilder component which can't be changed, hence the C++
to COM to C# process.
Can you please post the affected code of both the C# and the C++ side?
C# code fragment...

public int Search( string searchString )
{
Trace.TraceInformation( "Managed Search method invoked..." );

int status = -1;

return status;
}

C++ code fragment...

#include "stdafx.h"

#import "Test.tlb" raw_interfaces_only

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
CoInitialize( NULL );
return TRUE;
}

int Search( BSTR searchString )
{
long lResult = 0;

Test::ISearchEnginePtr searchEngine( __uuidof( Test::SearchEngine ) );

searchEngine->Search( searchString, &lResult );

printf( "result %d", lResult );

return lResult;
}


--Johannes


--
Johannes Passing - http://int3.de/
Thanks

Glenn





--
Johannes Passing - http://int3.de/



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.