5

Hello I'm currently running Visual Studio 2010 and have a context menu shell extension completely working in 32 bit on a 32 bit machine so all the methods exist. It is an ATL project. No errors or even warnings on the 32 bit.

Here is the issue. When i go into the configuration Manager under visual studio and switch the active solution platform from Win32 to x64 and try to compile i get the error "error C2259: 'ATL::CCOMObject : cannot instantiate abstract class".

Since this exact same project does compile and run in 32bit why does it throw me that error for x64?

Any Ideas or a point in the right direction would be appreciated.
Main Methods that are required and are implemented as follows:

STDMETHODIMP Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY);
STDMETHODIMP GetCommandString(UINT, UINT, UINT*, LPSTR, UINT);
STDMETHODIMP InvokeCommand(LPCMINVOKECOMMANDINFO);
STDMETHODIMP QueryContextMenu(HMENU, UINT, UINT, UINT, UINT);

To save Code Space Create an Atl Project. Once initial items have been created add a new class "TestingContextMenu" Rest of Code will be referencing this.

stdafk.h

#include "resource.h"
#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
#include <shlobj.h>
#include <comdef.h>

#include <string>
#include <list>
typedef std::list< std::basic_string<TCHAR> > string_list;

TestingContextMenu.h Only parts that have been added/Changed will be included

#include "stdafx.h"
using namespace std;
class ATL_NO_VTABLE CTestingContextMenu:
        public CComObjectRootEx<CComSingleThreadModel>,
    public CComCoClass<CTestingContextMenu, &CLSID_TestingContextMenu>,
    public IShellExtInit,
    public IContextMenu
    {
        // Comment out or remove IDispatch
BEGIN_COM_MAP(CMainMagnimbusContextMenu)
    //COM_INTERFACE_ENTRY(ITestingContextMenu)
    //COM_INTERFACE_ENTRY(IDispatch)
    COM_INTERFACE_ENTRY(IShellExtInit)
    COM_INTERFACE_ENTRY(IContextMenu)
END_COM_MAP()

protected:
    TCHAR m_szFile[MAX_PATH];
    list<string> Filenames;
    list<string> FilenamesCopier;
public:
    STDMETHODIMP Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY);

    STDMETHODIMP GetCommandString(UINT, UINT, UINT*, LPSTR, UINT);
    STDMETHODIMP InvokeCommand(LPCMINVOKECOMMANDINFO);
    STDMETHODIMP QueryContextMenu(HMENU, UINT, UINT, UINT, UINT);
};  //There is other code within this but it is autogenerated

TestingContextMenu.cpp

#include "stdafx.h"
#include "TestingContextMenu"
#include <sstream>
using namespace std;
#pragma comment(lib, "comsuppw")

STDMETHODIMP CMainMagnimbusContextMenu::Initialize ( 
  LPCITEMIDLIST pidlFolder,
  LPDATAOBJECT pDataObj,
  HKEY hProgID )
  {
  FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
    STGMEDIUM stg = { TYMED_HGLOBAL };
    HDROP     hDrop;

    if ( FAILED( pDataObj->GetData ( &fmt, &stg ) ))
        return E_INVALIDARG;
    hDrop = (HDROP) GlobalLock ( stg.hGlobal );

    UINT uNumFiles = DragQueryFile ( hDrop, 0xFFFFFFFF, NULL, 0 );
   HRESULT hr = S_OK;

   if ( 0 == uNumFiles )
    {
       GlobalUnlock ( stg.hGlobal );
       ReleaseStgMedium ( &stg );
       return E_INVALIDARG;
    }

   UINT counter = 0;
    // Get the name of the every file and store it in our member variable m_szFile.
   for(counter = 0; counter < uNumFiles; counter++)
   {
        if ( 0 == DragQueryFile ( hDrop, counter, m_szFile, MAX_PATH ) )
        {   
            hr = E_INVALIDARG;
        }
        wchar_t* t = _wcsdup(m_szFile);
        char ch[260];
        char DefChar = ' ';
        WideCharToMultiByte(CP_ACP,0,t,-1, ch,260,&DefChar, NULL);
        string ss(ch);
        Filenames.push_back(ss);
        FilenamesCopier.push_back(ss);
   }

   GlobalUnlock ( stg.hGlobal );
   ReleaseStgMedium ( &stg );

   return hr;
}

The Rest of the Functions are Availible upon request. However I have noticed something new. If you have just the above function and code implemented and the Config manager set to build x64 you get the initial error I am having. This even means not implementing the QueryContextMenu, GetCommandString, or the invoke command. Only error you'll get with this set up is my original which is what we would expect since they are not implemented. However switch that config manager back to Win32 and you get the expected errors such as 3 unresolved externals, and 3 errors following it naming GetCommandString, InvokeCommand, and QueryContextMenu. Again expected if they aren't implemented but why does the compiler on x64 recognize only my original error which is what a lot of people would assume it is, not implemented methods, but on the win32 set it shows the full errors when not implemented.

The previous paragraph was just something I noticed. I do have all 3 methods implemented correctly and compiles correctly in Win32 but not x64.

Rob
  • 63
  • 7
  • What is the rest of the error message? Usually there is a list of unimplemented members. Otherwise, post a minimal repro that demonstrates the problem. – James McNellis Aug 01 '12 at 21:55
  • That is the entire error message. Occurs within the altcom.h at this line ATLTRY(p = new T1(pv)) – Rob Aug 01 '12 at 22:31
  • 1
    Look in the Output window for the full message, it tells you which member of the class is the troublemaker. – Hans Passant Aug 01 '12 at 23:22
  • Thanks for Looking in output window, Following is a list, IContextMenu::GetCommandString(): is abstract T1=ATL::CComObject CreateInstance Why is this different from Win32 to x64? Exact same code compiles and runs correctly with Win32. set in Config Manager – Rob Aug 01 '12 at 23:30
  • Pro tip: C++ compilation errors can be lengthy--dozens or hundreds of lines long in some cases. The Error List window in Visual Studio shows only the first line of every error, warning, and message. It's always a good idea to check the Output window, which will show the entire output from the compiler. Most of the time only the first line is required to diagnose a problem, but when it isn't, there's a good chance that there may be extra information in the output. – James McNellis Aug 02 '12 at 18:26
  • Appreciate it. Too used to C# I guess. It spoiled me. – Rob Aug 02 '12 at 20:26

1 Answers1

6

Your GetCommandString parameters do not match those defined by the interface method.

Your

STDMETHODIMP GetCommandString(UINT, UINT, UINT*, LPSTR, UINT)

needs to be

STDMETHODIMP GetCommandString(UINT_PTR, UINT, UINT*, LPSTR, UINT)

In Win32 the mismatch is not so much important (the parameter types resolve to the same type), and in x64 is becomes important. Compiler build output should have given you a hint on this, including the missing method name.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • That was the issue. The error was not showing up for get command string. I read Passants comment and added the new info. Tried the fix you suggest and it works. Compiles and program has been tested and operating on a 64bit machine. Learning so much about c++ from these errors. I would upvote both you and Passant if i had the rep. – Rob Aug 02 '12 at 14:45