3,245
社区成员
发帖
与我相关
我的任务
分享
--------------------------------------------------------------------------------
.H
--------------------------------------------------------------------------------
// GPSReader.h : Declaration of the CGPSReader
...
class ATL_NO_VTABLE CGPSReader :
public IObjectSafetyImpl<CGPSReader, INTERFACESAFE_FOR_UNTRUSTED_CALLER| INTERFACESAFE_FOR_UNTRUSTED_DATA>,
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<CGPSReader, &CLSID_GPSReader>,
public IConnectionPointContainerImpl<CGPSReader>,
public CProxy_IGPSReaderEvents<CGPSReader>,
public IObjectWithSiteImpl<CGPSReader>,
public IDispatchImpl<IGPSReader, &IID_IGPSReader, &LIBID_GPSLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
CGPSReader()
{
}
#ifndef _CE_DCOM
DECLARE_REGISTRY_RESOURCEID(IDR_GPSREADER)
#endif
BEGIN_COM_MAP(CGPSReader)
COM_INTERFACE_ENTRY(IGPSReader)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IConnectionPointContainer)
COM_INTERFACE_ENTRY(IObjectWithSite)
COM_INTERFACE_ENTRY(IObjectSafety)
END_COM_MAP()
BEGIN_CONNECTION_POINT_MAP(CGPSReader)
CONNECTION_POINT_ENTRY(__uuidof(_IGPSReaderEvents))
END_CONNECTION_POINT_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
public:
STDMETHOD(GetGPSinfo)(LONG * m_gpsInfo); //对外的函数
};
OBJECT_ENTRY_AUTO(__uuidof(GPSReader), CGPSReader)
--------------------------------------------------------------------------------
CPP
--------------------------------------------------------------------------------
// GPSReader.cpp : Implementation of CGPSReader
#include "stdafx.h"
#include "GPSReader.h"
STDMETHODIMP CGPSReader::GetGPSinfo(LONG * m_gpsInfo) //直接赋值就返回了
{
// TODO: Add your implementation code here
*m_gpsInfo = 1;
return S_OK;
}
--------------------------------------------------------------------------------
HTML文件
--------------------------------------------------------------------------------
<HTML>
<HEAD>
<TITLE>GPS Test</TITLE>
<OBJECT
ID="GPS"
CLASSID="clsid:53A7AFB1-E3EE-4D7A-90BF-3D466B12A4ED"
WIDTH=0
HEIGHT=0
>
</OBJECT>
<script language="javascript">
function doTest()
{
alert("Start...");
var sum = GPS.GetGPSinfo(); //到了这里就无法运行了
alert(sum);
}
</script>
</HEAD>
<BODY>
<input type="button" value="TestGPS" id="btnOK" onclick="doTest()"></input>
</BODY>
</HTML>
--------------------------------------------------------------------------------
IDL文件
--------------------------------------------------------------------------------
...
library GPSLib
{
importlib("stdole2.tlb");
interface IDocHostUIHandlerDispatch;
interface IAxWinAmbientDispatchEx;
[
uuid(53A7AFB1-E3EE-4D7A-90BF-3D466B12A4ED), //HTM中用的是这个ID
helpstring("_IGPSReaderEvents Interface")
]
dispinterface _IGPSReaderEvents
{
properties:
methods:
};
[
uuid(121573D2-480D-468C-ADB5-2935B440523E),
helpstring("GPSReader Class")
]
coclass GPSReader
{
[default] interface IGPSReader;
[default, source] dispinterface _IGPSReaderEvents;
};
};
// GPS.idl : IDL source for GPS
//
// This file will be processed by the MIDL tool to
// produce the type library (GPS.tlb) and marshalling code.
import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(454F78B0-086F-4B81-88DD-55E43A6009F4),
dual,
nonextensible,
helpstring("IGPSReader Interface"),
pointer_default(unique)
]
interface IGPSReader : IDispatch{
[id(1), helpstring("method GetGPSinfo")]
HRESULT GetGPSinfo( [out,retval] LONG * m_gpsInfo);
};
[
uuid(C9C53C58-3B3D-4A9B-8ED1-88FCFB5942C8),
version(1.0),
helpstring("GPS 1.0 Type Library")
]
library GPSLib
{
importlib("stdole2.tlb");
interface IDocHostUIHandlerDispatch;
interface IAxWinAmbientDispatchEx;
[
uuid(53A7AFB1-E3EE-4D7A-90BF-3D466B12A4ED),
helpstring("_IGPSReaderEvents Interface")
]
dispinterface _IGPSReaderEvents
{
properties:
methods:
};
[
uuid(121573D2-480D-468C-ADB5-2935B440523E),
helpstring("GPSReader Class")
]
coclass GPSReader
{
[default] interface IGPSReader;
[default, source] dispinterface _IGPSReaderEvents;
};
};
// GPS.idl : IDL source for GPS
//
// This file will be processed by the MIDL tool to
// produce the type library (GPS.tlb) and marshalling code.
import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(454F78B0-086F-4B81-88DD-55E43A6009F4),
dual,
nonextensible,
helpstring("IGPSReader Interface"),
pointer_default(unique)
]
interface IGPSReader : IDispatch{
[id(1), helpstring("method GetGPSinfo")]
HRESULT GetGPSinfo( [out,retval] LONG * m_gpsInfo);
};
[
uuid(C9C53C58-3B3D-4A9B-8ED1-88FCFB5942C8),
version(1.0),
helpstring("GPS 1.0 Type Library")
]
library GPSLib
{
importlib("stdole2.tlb");
interface IDocHostUIHandlerDispatch;
interface IAxWinAmbientDispatchEx;
[
uuid(53A7AFB1-E3EE-4D7A-90BF-3D466B12A4ED),
helpstring("_IGPSReaderEvents Interface")
]
dispinterface _IGPSReaderEvents
{
properties:
methods:
};
[
uuid(121573D2-480D-468C-ADB5-2935B440523E),
helpstring("GPSReader Class")
]
coclass GPSReader
{
[default] interface IGPSReader;
[default, source] dispinterface _IGPSReaderEvents;
};
};
CLASSID="clsid:53A7AFB1-E3EE-4D7A-90BF-3D466B12A4ED"