USING MapWinGIS with MFC

I have Type Mismatch error while using AddLayerFromFilename function. I attach the section of my code here.

void ::OnMapobjectAddlayer()
{
    CString filepath = OpenLayerFile(false);
    BOOL fe = PathFileExists(filepath);
    if(!fe)
        {
        AfxMessageBox(_T("New file, will create a new shapefile layer"));
        MapWinGIS::IShapefilePtr newlayer;
        newlayer.CreateInstance(__uuidof(Shapefile));
        VARIANT_BOOL createFlag = newlayer->CreateNew(_bstr_t(filepath), ShpfileType::SHP_POLYLINE);
        m_layerHandle = m_map.AddLayer(newlayer, true);
        newlayer->Save(nullptr);
        }
    else
        {
	    // TODO: Add your command handler code here
		m_map.put_Projection(tkMapProjection::PROJECTION_GOOGLE_MERCATOR);
		m_map.put_GrabProjectionFromData(TRUE);
		m_map.put_ZoomBehavior(tkZoomBehavior::zbUseTileLevels);
		USES_CONVERSION;
		LPWSTR s2 = A2W(filepath);
		//LPWSTR temp = (LPWSTR)filepath.GetBuffer();
		m_layerHandle = m_map.AddLayerFromFilename(s2, (long)MapWinGIS::tkFileOpenStrategy::fosVectorLayer, true);
        }
   m_map.Redraw();
  }

Also please how may I use Filemanager

		MapWinGIS::IFileManagerPtr fm;
		fm.CreateInstance(__uuidof(FileManager));
		BSTR pVal;
		fm->get_ErrorMsg(fm->GetLastErrorCode(), &pVal);
		AfxMessageBox(LPCTSTR(pVal));

Thank you.

I got solution with FileManager as follows:

void ::OnMapobjectAddlayer()
{

 	_MapWinGIS::IFileManagerPtr fm;
	fm.CreateInstance(__uuidof(_MapWinGIS::FileManager));
	BSTR pVal;
	VARIANT_BOOL vBool, bSupported;
    CString filepath = OpenLayerFile(false);
    BOOL fe = PathFileExists(filepath);
	BSTR bStr = filepath.AllocSysString();

    if(!fe)
        {
        AfxMessageBox(_T("New file, will create a new shapefile layer"));
        _MapWinGIS::IShapefilePtr newlayer;
        newlayer.CreateInstance(__uuidof(_MapWinGIS::Shapefile));
        VARIANT_BOOL createFlag = newlayer->CreateNew(bStr, _MapWinGIS::ShpfileType::SHP_POLYLINE);
        m_layerHandle = m_map.AddLayer(newlayer, true);
        newlayer->Save(nullptr);
        }
    else
        {

		HRESULT hResult = fm->get_IsSupported(bStr, &bSupported);
		if (bSupported == VARIANT_FALSE)
		{
			AfxMessageBox(_T("Datasource isn't supported by MapWinGIS"));
			::SysFreeString(bStr); //finished using the BSTR
			return;
		}
		else
		{
			LPDISPATCH obj = fm->Open(bStr, _MapWinGIS::tkFileOpenStrategy::fosAutoDetect, nullptr);
			/*
			if (obj != nullptr && fm->LastOpenIsSuccess)
			{
				m_layerHandle = m_map.AddLayer(obj, true);
			}
			*/
				if (fm->LastOpenIsSuccess)
				{
					if (fm->LastOpenStrategy == _MapWinGIS::tkFileOpenStrategy::fosVectorLayer)
					{
						_MapWinGIS::IShapefilePtr shpfile;
						shpfile.CreateInstance(__uuidof(_MapWinGIS::Shapefile));
						shpfile = (IShapefilePtr)obj;
						if (shpfile != nullptr)
							m_layerHandle = m_map.AddLayer(shpfile, true);
					}
					else
					{
						_MapWinGIS::IImagePtr image;
						image.CreateInstance(__uuidof(_MapWinGIS::Image));
						image = (IImagePtr)obj;
						if (image != nullptr)
							m_layerHandle = m_map.AddLayer(image, true);
					}

				}
				else
				{
					fm->get_ErrorMsg(fm->LastErrorCode, &pVal);
					CString str(pVal);
					AfxMessageBox(_T("Failed to open datasource: ") + str);
					return;
				}
			}

	}
   
	::SysFreeString(bStr); //finished using the BSTR
        m_map.Redraw();
}

The problem is in calling fm->get_IsSupported(bStr, &bSupported) as if returning BOOLEAN; It return S_OK. The solution is to check bSupported for supported shapefiles;

Hello @KehindeIsijola

Yes, that’s correct. All COM calls from c++ return an HRESULT, and you check for S_OK. The return value of all functions (as you would see from a high-level language) will always instead be the last parameter when called from c++.

Regards,
Jerry.

Thanks for the confirmation.
Regards.
@KehindeIsijola

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.