[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

08/09/2009 at 10:30PM PDT, ID: 24639043
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.8

64-bit code calls 32-bit DLL through ATL COM adapter but gets 64-bit version, not 32-bit

Asked by EdAtCertFirst in Microsoft Visual C++, Windows ATL / WTL / COM Programming, Windows 64-bit

Tags: COM, ATL, 64-bit, x64

My 64-bit code needs to call 32-bit DLL code that cannot be recompiled in 64-bit (bought DLL, not the source). I wrote an adapter using ATL/COM out of process server. I am using Visual Studio 2008, C++.
My 64-bit client works with the adapter compiled in 64-bit mode and when both are compiled in 32-bit mode, they work. I am looking for the magic function call, parameter, or registry modification to make the 64-to-32-bit combination work. The two articles in MSDN about 64-bit to 32-bit calls do not help.
The adapter is an out of process COM server, an EXE written with ATL.
I have a .zip file of my code but even the .sln file is not allowed in a .zip by exp-xch.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
// Client2.cpp				2009-08-06
//
//		Test driver for COM Adapter3 to access the Calculator DLL
//
#include <windows.h>
#include <objbase.h>
#include <iostream>
#include <iomanip>
using namespace std;
 
#include <atlbase.h>
#include <atldef.h>
#include <windef.h>
#include "ComAdapter3_i.h"
 
///////////////////////////// Interface code for accessing the COM Adapter ///////////////////////////////////////
 
int main()
{
	// Initialize COM
 
	HRESULT hr = 0;
	LPUNKNOWN pServer = 0;
	ICoCalc* calc = 0;
 
	::CoInitialize(NULL);
 
// Tried this:
	//PVOID sv = 0;
	//Wow64DisableWow64FsRedirection(&sv);
 
	hr = CoCreateInstance(CLSID_CoCalc, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (LPVOID*) &pServer);
	if (FAILED(hr))
	{
		cout << "Failed to load server\n";
		goto cleanup;
	}
 
	hr = pServer->QueryInterface(IID_ICoCalc, (LPVOID*)&calc);
	if (FAILED(hr))
	{
		cout << "ICalc not found" << endl;
		goto cleanup;
	}
 
	////////////////////////// Normal Application Code //////////////////////////////
 
	cout << "\n\tCalcTest\n\n\n";
 
	long a = 0, b = 0, c = 0;
 
	while (cout << "Enter two integers or Q to quit: ", cin >> a >> b)
	{
		// COM keeps the function addresses in the VTable, makes virtual function calls
		c = calc->Add(a, b);		// a proxy layer could hide calc pointer
		cout << "Sum = " << c << endl;
///  I modified Add to just:   return 8 * sizeof(size_t);
///  It returns 32 or 64 to show what version of code is running in the "COM server"
		cout << "\nClient calc: " << calc << endl;
		cout << "Add: a: " << a << "  &a: " << &a << endl;
		cout << "     b: " << b << "  &b: " << &b << endl;
		cout << "     c: " << c << "  &c: " << &c <<endl;
	}
 
cleanup:		// Cannot build a new ComServer until its usage count is zero.
	if (calc) 
		hr = calc->Release();
	if (pServer)
		hr = pServer->Release();
	CoUninitialize();
	return hr;
}
 
 
 
// CoCalc.cpp : Implementation of CCoCalc
//
//	Test program with local Add function representing code that would be in 32-bit DLL.
//	Calling the DLL will be easy after the 64-32 barrier is fixed.
//
//  When launching Visual Studio, run as Administrator so that various build steps will work.
//  Build ComAdapter3. Then build ComAdapter3PS by right-clicking on that Project name and selecting
//  Project only > Rebuild on ComAdapter3PS.
//
//  Next, go to Client3 directory and run COPY.BAT from a command line window. It copies some generated files.
//  I like keeping the client and server separated.
//  Then build the client3 program and run it.
//  Be careful to check that client is 64-bits and Adapter is 32-bits. Test shows 64-bit Adapter is loaded, matching the
//  bit-ness of the client. I need to turn off the direct matching and access the 32-bit version.
//  The code is basically correct, just missing the magic instructions to override that mapping.
//  Building both Client and Adapter in 32 or 64-bit mode shows correct 32 or 64 output.
 
#include "stdafx.h"
#include "CoCalc.h"
#include <cstddef>
#include <cstdlib>
 
// CCoCalc
 
STDMETHODIMP CCoCalc::Add(LONG a, LONG b)
{
	long c = a + b;
 
	c = sizeof(size_t) * 8;		// report 32-bit or 64-bit version instead of sum to prove the 64 to 32 call works
 
	return c;
}
[+][-]08/10/09 05:14 AM, ID: 25059163

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]08/10/09 06:33 AM, ID: 25059741

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/10/09 09:41 PM, ID: 25066182

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/11/09 09:12 PM, ID: 25075712

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08/14/09 08:37 PM, ID: 25104034

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08/16/09 06:54 PM, ID: 25111264

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/18/09 10:23 PM, ID: 25129793

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08/19/09 03:04 AM, ID: 25130982

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/21/09 07:26 AM, ID: 25152114

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C++, Windows ATL / WTL / COM Programming, Windows 64-bit
Tags: COM, ATL, 64-bit, x64
Sign Up Now!
Solution Provided By: EdAtCertFirst
Participating Experts: 1
Solution Grade: A
 
 
[+][-]08/21/09 07:33 AM, ID: 25152194

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91 - Hierarchy / EE_QW_3_20080625