Calling unmanaged code from managed MFC worker thread

Hi,

I have an existing VC 6 MFC application which communicates asynchronly with 
a VC 2005 managed code dll.
I use an unmanaged base class with virtual functions to access methods in 
the MFC application.
Furthermore, I use a pointer to an unmanaged function to jump back into the 
managed dll.

The managed part is basically a remoting enhancement which asynchronly 
initiates a call from the remoting thread to the MFC application. The 
information is collected within the main thread of the MFC application. The 
managed code dll is invoked via a function pointer. From the managed code 
additional calls to the unmanaged MFC application will be done via a pointer 
to an object in the MFC. When this code is executed within the main thread 
of the MFC application everything works fine.

However, if I switch to a MFC worker thread, calls via the base class 
pointer results in System.AccessViolationException!

Scenario, which works:
1. Remoting thread imvoked
2. Managed DLL -> MFC App asynchron, no result
3. Switch thread to App main thread
4. MFC App -> Managed DLL
5. Managed DLL -> MFC App (synchron)

Scenario, which does not work:
1. Remoting thread imvoked
2. Managed DLL -> MFC App (asynchron, no result)
3. Switch thread to MFC worker thread!!!
4. MFC App -> Managed DLL
5. Managed DLL -> MFC App (synchron): System.AccessViolationException !!

How can I access pointer to unmanaged classes from managed code within a 
worker thread?

Best regards,
Klaus 


0
Klaus1 (3)
8/1/2007 7:34:11 PM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
770 Views

Similar Articles

[PageSpeed] 34

On Aug 1, 12:34 pm, "Klaus" <Kl...@nowhere.invalid> wrote:
> Hi,
>
> I have an existing VC 6 MFC application which communicates asynchronly with
> a VC 2005 managed code dll.
> I use an unmanaged base class with virtual functions to access methods in
> the MFC application.
> Furthermore, I use a pointer to an unmanaged function to jump back into the
> managed dll.
>
> The managed part is basically a remoting enhancement which asynchronly
> initiates a call from the remoting thread to the MFC application. The
> information is collected within the main thread of the MFC application. The
> managed code dll is invoked via a function pointer. From the managed code
> additional calls to the unmanaged MFC application will be done via a pointer
> to an object in the MFC. When this code is executed within the main thread
> of the MFC application everything works fine.
>
> However, if I switch to a MFC worker thread, calls via the base class
> pointer results in System.AccessViolationException!
>
> Scenario, which works:
> 1. Remoting thread imvoked
> 2. Managed DLL -> MFC App asynchron, no result
> 3. Switch thread to App main thread
> 4. MFC App -> Managed DLL
> 5. Managed DLL -> MFC App (synchron)
>
> Scenario, which does not work:
> 1. Remoting thread imvoked
> 2. Managed DLL -> MFC App (asynchron, no result)
> 3. Switch thread to MFC worker thread!!!
> 4. MFC App -> Managed DLL
> 5. Managed DLL -> MFC App (synchron): System.AccessViolationException !!
>
> How can I access pointer to unmanaged classes from managed code within a
> worker thread?
>
> Best regards,
> Klaus


Hi, Klaus,

I assume you are already using delegates, and marshaling of delegate
method pointers to access unmanaged functions from managed code,
correct?

http://msdn2.microsoft.com/en-us/library/bfeyhsdy.aspx




0
8/1/2007 8:27:34 PM
Thanks for your help!

Here is the code:
myHeader.h, shared in both projects, MFC 6.0 App and VC 2005 DLL:
class CMyBase
{
public:
 CMyBase() {};
 virtual ~CMyBase() {};
 virtual bool MyExample() = 0;
};

In MFC App this class will be derived:
class CMyBaseDerived : public CMyBase
{
public:
 CMyBaseDerived();
 virtual ~CMyBaseDerived();
 virtual bool MyExample();
};

In VC2005, I have a pointer to the instance of CMyBaseDerived:
CMyBase *PMyBase;
This pointer will be initialized by calling a function in the DLL from the 
App:
void MyInit(CMyBase *p)
{
    PMyBase = p;
}

When I use this pointer in the DLL from the MFC worker thread an 
AccessViolationException will be thrown:
PMyBase->MyExample();

Using this pointer within the main thread no problem occurs.

Best regards,
Klaus


"Pixel.to.life" <pixel.to.life@gmail.com> wrote in message 
news:1186000054.219137.299310@i38g2000prf.googlegroups.com...
> On Aug 1, 12:34 pm, "Klaus" <Kl...@nowhere.invalid> wrote:
>> Hi,
>>
>> I have an existing VC 6 MFC application which communicates asynchronly 
>> with
>> a VC 2005 managed code dll.
>> I use an unmanaged base class with virtual functions to access methods in
>> the MFC application.
>> Furthermore, I use a pointer to an unmanaged function to jump back into 
>> the
>> managed dll.
>>
>> The managed part is basically a remoting enhancement which asynchronly
>> initiates a call from the remoting thread to the MFC application. The
>> information is collected within the main thread of the MFC application. 
>> The
>> managed code dll is invoked via a function pointer. From the managed code
>> additional calls to the unmanaged MFC application will be done via a 
>> pointer
>> to an object in the MFC. When this code is executed within the main 
>> thread
>> of the MFC application everything works fine.
>>
>> However, if I switch to a MFC worker thread, calls via the base class
>> pointer results in System.AccessViolationException!
>>
>> Scenario, which works:
>> 1. Remoting thread imvoked
>> 2. Managed DLL -> MFC App asynchron, no result
>> 3. Switch thread to App main thread
>> 4. MFC App -> Managed DLL
>> 5. Managed DLL -> MFC App (synchron)
>>
>> Scenario, which does not work:
>> 1. Remoting thread imvoked
>> 2. Managed DLL -> MFC App (asynchron, no result)
>> 3. Switch thread to MFC worker thread!!!
>> 4. MFC App -> Managed DLL
>> 5. Managed DLL -> MFC App (synchron): System.AccessViolationException !!
>>
>> How can I access pointer to unmanaged classes from managed code within a
>> worker thread?
>>
>> Best regards,
>> Klaus
>
>
> Hi, Klaus,
>
> I assume you are already using delegates, and marshaling of delegate
> method pointers to access unmanaged functions from managed code,
> correct?
>
> http://msdn2.microsoft.com/en-us/library/bfeyhsdy.aspx
>
>
>
> 


0
Klaus1 (3)
8/2/2007 9:14:19 AM
On Aug 2, 2:14 am, "Klaus" <Kl...@nowhere.invalid> wrote:
> Thanks for your help!
>
> Here is the code:
> myHeader.h, shared in both projects, MFC 6.0 App and VC 2005 DLL:
> class CMyBase
> {
> public:
>  CMyBase() {};
>  virtual ~CMyBase() {};
>  virtual bool MyExample() = 0;
>
> };
>
> In MFC App this class will be derived:
> class CMyBaseDerived : public CMyBase
> {
> public:
>  CMyBaseDerived();
>  virtual ~CMyBaseDerived();
>  virtual bool MyExample();
>
> };
>
> In VC2005, I have a pointer to the instance of CMyBaseDerived:
> CMyBase *PMyBase;
> This pointer will be initialized by calling a function in the DLL from the
> App:
> void MyInit(CMyBase *p)
> {
>     PMyBase = p;
>
> }
>
> When I use this pointer in the DLL from the MFC worker thread an
> AccessViolationException will be thrown:
> PMyBase->MyExample();
>
> Using this pointer within the main thread no problem occurs.
>
> Best regards,
> Klaus
>
> "Pixel.to.life" <pixel.to.l...@gmail.com> wrote in message
>
> news:1186000054.219137.299310@i38g2000prf.googlegroups.com...
>
>
>
> > On Aug 1, 12:34 pm, "Klaus" <Kl...@nowhere.invalid> wrote:
> >> Hi,
>
> >> I have an existing VC 6 MFC application which communicates asynchronly
> >> with
> >> a VC 2005 managed code dll.
> >> I use an unmanaged base class with virtual functions to access methods in
> >> the MFC application.
> >> Furthermore, I use a pointer to an unmanaged function to jump back into
> >> the
> >> managed dll.
>
> >> The managed part is basically a remoting enhancement which asynchronly
> >> initiates a call from the remoting thread to the MFC application. The
> >> information is collected within the main thread of the MFC application.
> >> The
> >> managed code dll is invoked via a function pointer. From the managed code
> >> additional calls to the unmanaged MFC application will be done via a
> >> pointer
> >> to an object in the MFC. When this code is executed within the main
> >> thread
> >> of the MFC application everything works fine.
>
> >> However, if I switch to a MFC worker thread, calls via the base class
> >> pointer results in System.AccessViolationException!
>
> >> Scenario, which works:
> >> 1. Remoting thread imvoked
> >> 2. Managed DLL -> MFC App asynchron, no result
> >> 3. Switch thread to App main thread
> >> 4. MFC App -> Managed DLL
> >> 5. Managed DLL -> MFC App (synchron)
>
> >> Scenario, which does not work:
> >> 1. Remoting thread imvoked
> >> 2. Managed DLL -> MFC App (asynchron, no result)
> >> 3. Switch thread to MFC worker thread!!!
> >> 4. MFC App -> Managed DLL
> >> 5. Managed DLL -> MFC App (synchron): System.AccessViolationException !!
>
> >> How can I access pointer to unmanaged classes from managed code within a
> >> worker thread?
>
> >> Best regards,
> >> Klaus
>
> > Hi, Klaus,
>
> > I assume you are already using delegates, and marshaling of delegate
> > method pointers to access unmanaged functions from managed code,
> > correct?
>
> >http://msdn2.microsoft.com/en-us/library/bfeyhsdy.aspx- Hide quoted text -
>
> - Show quoted text -


Klaus,

First of all, it doesnt not seem like your MFC app is a managed
aplpication. Do you compile it with /clr command line option?
If you are using only MFC, why do you need to build a managed
application?

Secondly, do you have the method MyExample() overridden in
CMyBaseDerived?

Thirdly, assuming you have MyExample() overridden in CMyBaseDerived,
dont you have to cast PMyBase to a pointer to the derived object?
Virtual method references are resolved at run time, so unless the
machine knows the object is of derived type, it would look for the
method's definition ion the base class (
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.3
).. and if you dont have an implementation of MyExample() in the base
class, you will get an access violation.. no matter in managed/
unmanaged code.

Pardon me if I missed something.

0
8/2/2007 11:59:03 PM
Hi,

The MFC App is pure unmanaged, built with VC 6.0!
MyExample() must be overriden in CMyBaseDerived, otherwise it could not be 
instantiated.
From my understanding, I do not have to cast the pointer to the derived 
class in order to call MyExample; this is exactly why the concept of virtual 
methods was introduced; your web reference states nothing else.

Don't forget, everything works fine in the main thread!
So, from my point of view it is more a "security issue" in managed code 
preventing me from calling unsecure code?

Best regards,
Klaus


0
Klaus1 (3)
8/3/2007 10:23:08 AM
On Aug 3, 3:23 am, "Klaus" <Kl...@nowhere.invalid> wrote:
> Hi,
>
> The MFC App is pure unmanaged, built with VC 6.0!
> MyExample() must be overriden in CMyBaseDerived, otherwise it could not be
> instantiated.
> From my understanding, I do not have to cast the pointer to the derived
> class in order to call MyExample; this is exactly why the concept of virtual
> methods was introduced; your web reference states nothing else.
>
> Don't forget, everything works fine in the main thread!
> So, from my point of view it is more a "security issue" in managed code
> preventing me from calling unsecure code?
>
> Best regards,
> Klaus

Klaus,

Then I did not understand your problem properly, my apologies.

Good luck with it.

Pixel.To.Life.

0
8/3/2007 4:29:25 PM
Reply:

Similar Artilces:

WinINet error (code 12110
Hi, When I try to publish my web pages I get the message: An unknown WinINet error has occurred (code 12110). If I click OK and publish again the error will appear again. If I keep trying publish, OK..... my pages will finally upload. Why do I get this error and how do I resolve the problem? Your help would be appreciated. Thanks, Phil That error means the ftp session is already in progress. Are you uploading large files? -- ~~~~~~~~~~~~~~~~~~ Rob Giordano Microsoft MVP Expression Web "Phillip" <Phillip@discussions.microsoft.com> wrote in mes...

Covered Call Options
Hello all, I do I go about dealing with a covered call transaction in Microsoft Money 2007 Premium? Currently what I am doing is inputing the option symbol, and choosing the transaction "Sell to Open" then when I go to expire the option I try to do a Sell to Close. However it does not seem to work? Also what happens if instead of expired I has the position assigned? how would I account for that? Thank you. Shhhh In microsoft.public.money, Shhhh wrote: > >I do I go about dealing with a covered call transaction in Microsoft >Money 2007 Premium? Currently what I am doing is...

After installing multiple updates, error code 0xC80001F
I have been installing multiple updates (due to computer restore). During this last set of updates, I noticed that there were a few that said they weren't installed (failed) successfully. I went back to the installed updates info, but there were no items that showed "failed". When I tried to go back to the Windows Update Home page, I received this error code [Error number: 0xC80001FE]. I do not have a 3rd party anti-virus or firewall active right now. I have done nothing following the installation of updates. -- Real integrity is doing the right thing, knowing tha...

Call in?
Do new owners of Office X have to call up Microsoft to register their copy? If yes, what's the number? thanks. Ming <asdf@asdf.com> wrote: > Do new owners of Office X have to call up Microsoft to register their > copy? If yes, what's the number? thanks. Nope. We're not using Windoze :->>> This version does not require activation. Corentin -- - Mac:MS MVP (Francophone) - (MS) MVP: http://support.microsoft.com/default.aspx?ln=FR&scid=fh;FR;mvp Newsgroups produits MS: http://support.microsoft.com/newsgroups/?ln=FR ...

Call center tracking and reporting
Anyone use CRM 4.0 for call tracking? Our Sales/Marketing is set up. Now I need to decide how to most efficiently customize CRM 4.0 for our company's call tracking portion. Can someone send me a link to information? thanks ...

Java code to force update of a field on a lead form
I'm trying to create workflows that notify me when a user hasn't contacted a lead for more than 2 days. Currently I'm doing this by checking the condition of the Qualified status after waiting for two days, however this isn't ideal as the lead might not be in a position to be Qualified or Unqualified at that time. I've put a last updated field on the lead form that automatically generates the date and time when a lead is saved but I can't seem to create a workflow that says 'if the last updated date is greater than 2 days trigger an event'. I think I cou...

Command button code
I have created a form, and within this form are several subforms. I need a command button which will take the data that is entered into a field in the main form and update it in the corrosponding field in the subforms (to avoid the user repeatedly having to fill this field out in the subforms). Can anyone tell me how to to this? Cheers Kirsten Tui, You don't have to... since the main form is related to each of the subform tables (Parent/Child), any value on the One side of the relationship can always be reassociated "on the fly" to the Many side in any subsequent ...

telephone calls
when placing a telephone call using outlook, everything works fine except I can't hear the person i am talking to. They can hear me find. When I use a telephone program not associated with outlook every thing works great. can someone help ...

Support Code:85010001
Howdy, I just purchase a Sprint PPC-6700 in the hopes that it would sync directly to my Exchange SP2 server. The error message that I receive is "Your account in Microsoft Exchange Server does not have permission to synchronize with your current settings. Contact your Exchange Server administrator. Support Code:850100001." Does anyone have a resolution to this problem? Thanks, Prayer Sorry, no help, but I'd love to hear what you think of the 6700 once you get it working! "Solanky, Prayer S." wrote: > Howdy, > > I just purchase a Sprint PPC-6700 i...

Calling Macro
Hi All, I have macro like this sub unique() '''''''' '''''''' Counter_item = 0 For Each Item In nodups counter_item = counter_item + 1 bic_var = Item Call Ps_Match Next Item end sub I have another macro Sub Ps_Match() ''''' ''''' If counter_item <= 1 Then call another_macro() end if end sub In the first macro i am declaring counter_item equals to 1. In my second macro i need to call that number. At the to of the module put: Public Counter_I...

CoCreateInstanceEx failed with error code 800706BA
Dear All My VB client on Win2k with SP4 is trying to connect to the remote server present on WINXP with SP2 when the client connects it gives the following error msg. CoCreateInstanceEx failed with error code 800706BA can anyone help me out as to why it happens. the server and client works fine when communicated from Win 2k to Win2k but in XP why such a problem comes. Regards Mary The firewall in WINXP SP2 might be blocking the calls. run the firewall.cpl applet and add your application to the list of authorized applications. -- Cheers Check Abdoul [VC++ MVP] --------------------...

Error code 0x800C0008 With Clip Art
Ilm running WIndows 7 and Outlook 2007 Ultimate has just been installed adn registered. When I try to use clip art with a globe beside it in the selection panel the programme, PowrPoint or Wrod freezes for a bit then I get the error code: Error code 0x800C0008 I've looked for solutions, added Microsoft Office to the trusted sites, cleared Cookies, and tried every other solution I've found on line but without success. Help! I've now wasted almost 3 hours!!!! Thanks Greg I presume you have read this: <http://support.microsoft.com/kb/912675> In...

color coding different data series
Hello! I have about 8 spreadsheets of data that I need to compile into one chart. However, I need to be able to look at the chart and see which spreadsheet a data point is from. Is there a quick way to color code a group of data points from one spreadsheet? I know I could change the color individually, but with over 50 data points, I'd rather not if I don't have to! Thank you. Hi, If I undserstand you correctly all you need to do is right click on one of the data points and select Format Data Series. You can then set the colour, line weight, pattern etc. as you want. Repeat f...

Error 430 in Access 2007
I have a strange situation: Till recently my automated process, reading from . csv file is working fine on my co-workers machine and all of sudden it is showing ERROR: Run-time error 430: Class does not support automation or does not support expected interface. I tried the following suggested ont his site: 1) Checked all the references and their order. 2) Uninstalled and reinstalled Access and Office package. 3) Checked for dao360.dll and it already exist. It works fiine on all machine except one. That one machine was re-installed with ACCESS\OFFICE again. Any thoughts\Suggestion...

Coding Inputmasks
Hi Group, I have what is probably a basic question that shows my (lack of) knowledge as to how forms and subforms interact. I have frmClients into which I enter various data. It has a primary key PacketNo. I also have a subform frmSpClients whcih contains similar data to frmClients and is linked by the primary key PacketNo. I have telephone fields in each form. In frmClients I have coded the text boxes for inputmasks dependent upon the number of digits entered. It works fine in frmClients, but it does strange things when I use the same code (with appropriate changes tot he contro...

determining mfc file dependencies
I have an old (mfc 4.2, built with msvc 6.0) activex application created with MFC. Can someone please tell me how I determine what file dependencies are necessary with this thing? I used to merely package it up without any mfc dlls......but, now I need to as whatever dlls this thing is dependent upon are not present in many newer systems! Thanks, Ike "Ike" <rxv@hotmail.com> wrote in message news:sHxKd.295$cl1.254@newsread3.news.pas.earthlink.net... >I have an old (mfc 4.2, built with msvc 6.0) activex application created > with MFC. Can someone please tell me how I de...

Microsoft Dynamics GP Report: Position Codes List
Accounting says HR cannot change the use of this field. The field should be used for position code Like "senior retail" . It was set up to track our codes for payroll percentages. Accounting says it drives g/l postings? The field links to over 80% of the HR package..hence the reason we are looking at a $200,000 package. Has anyone encounter this before? Ed Accounting is correct. Payroll tracks each of the position/manager/employees and has intrinsic code behind it that is used for Tax and posting reporting. Before you spend that $200,000 though...almost every module in G...

virus
Got that virus which inserts a window into my browsers (Opera and Firefox), suggesting to send a SMS somewhere to get e removal code - how is this virus called and what are the remedies? uninstalled browsers, run Dr Web CureIt, installed browsers - did not help "aa" <aa@microsoft.com> wrote in message news:uTqiPjftKHA.4568@TK2MSFTNGP05.phx.gbl... > Got that virus which inserts a window into my browsers (Opera and Firefox), > suggesting to send a SMS somewhere to get e removal code - how is this virus > called and what are the remedies? > > ...

Workflow -Assembly Call
How does add subtract in the Workflow>> assembly call works. I do understand that ypu can write your own custom workflows, register it and then call them from here, but I am pretty confused with the existing functionality. workflows can call other workflows workflows can also call a method in a .net assembly. ie you could write a piece of code in c# or vb.net and then have the workflow call your code. ======================= John O'Donnell Microsoft CRM MVP http://codegallery.gotdotnet.com/crm "SAM" <SAM@discussions.microsoft.com> wrote in message news:DE...

VBA code for my report
Hi, I would like to use a modified version of this VBA code for my report, but I’m not sure how. The code is from the 2007 sample Northwind db. It runs in the Quarterly report. Private Sub Report_Open(Cancel As Integer) On Error GoTo ErrorHandler Dim strSQL As String If IsNull(TempVars![Display]) Or IsNull(TempVars![Group By]) Or IsNull(TempVars![Year]) Then DoCmd.OpenForm "Sales Reports Dialog" Cancel = True Exit Sub End If strSQL = "TRANSFORM CCur(Nz(Sum([Sales]),0)) AS X" strSQL = strSQL & " SELECT [" &am...

Async calls to WebService in MFC
Hey guys, I use a webservice over the net which is scripted in VB.Net. This web service receives a username and a password and returns a string structure with multiples infos. If the string is empty, I had the wrong username and/or password. Pretty simple isn't? Ok now the problem is a bit more complex. I'm using an MFC client to connect to these web services, and since they're on the internet, there's a delay between sending the data and receiving an answer. I don't know really how I can tell me client to "wait for the string". The wrapper MFC created for the w...

Comments entered on Task Updates to Manager: Where does it get sto
There is a checkbox option on the My Tasks page for team members to select and send comments about task updates to their managers. Anyone know where these task comments can be reviewed other than when the PM opens it up to approve? It does not seem to be retained anywhere like the Notes get retained. Hi Kahuna, PM can see the comments with Approvals -> Task Updates -> Go To -> Applied Requests and Errors, clicking on the task name. The approval history is listed there including comments. A team member can see them with clicking in the task name in 'My Tasks&#...

Invalid procedure call or argument?
Hello All This is a re-post of a problem I posted a while ago, but to which I got no suggestions: hopefully I'll be more lucky this time!? I have the following code in a Private Sub: If InStr([ResponseXML], "element") > 0 Then errorfield = "Invalid " & Mid([ResponseXML], InStr([ResponseXML], "element"), InStr(InStr(InStr([ResponseXML], "element"), [ResponseXML], "'") + 1, [ResponseXML], "'") - InStr([ResponseXML], "element") + 1) Else .... Most of the time this works fine, but for some...

VBA code #2
I have a form that i want to show messages when the user clicks CONTINUE but im not sure how i can get the CONTINUE button to show the different messages. An example of what i have is below. When CONTINUE is clicked a second time LBLMSG1 should show a different message. Can anyone help? Private Sub Continue_Click() lblMsg1 = "Message 1" End Sub -- LB79 ------------------------------------------------------------------------ LB79's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=12156 View this thread: http://www.excelforum.com/showthread.php?thread...

MFC #2
hello This is a re-post, since none responded to it. I have automated power point from VC++ .I could save the ppt files to gif. presentation.Export("D:\\test","gif",100,100); But the gif files are named slide1, slide 2, slide3 etc. I want them to named with slide title. From the power point news group i got some VB code. But I couldn't find the appropriate function in VC++. To obtain the title in a variable sTitle they said use the VB code oSlide.Shapes.Title.TextFrame.TextRange.Text But i couldn't find the appropriate functions in VC++. priya C does...