求助:dll调用ocx问题

Call Next 2012-02-14 06:44:01
dll中的cpp文件

#include "stdafx.h"
#include "Call_client.h"
#include "test_MainTest.h"
#include <string>
#include "Sub.h"
#include "CDclient.h"
#include < atlbase.h >
#include "CReply.h"
#import "client.ocx"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

BEGIN_MESSAGE_MAP(CCall_clientApp, CWinApp)
END_MESSAGE_MAP()
// CCall_clientApp construction
CCall_clientApp::CCall_clientApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CCall_clientApp object
CCall_clientApp theApp;
// CCall_clientApp initialization
BOOL CCall_clientApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}

JNIEXPORT jstring JNICALL Java_test_MainTest_call_1queryPoints(JNIEnv *jenv, jclass jobj, jstring jstr)
{
typedef HRESULT (WINAPI * FREG)();
std::string str("Empty");
jstring result = jenv->NewStringUTF(str.c_str());
HMODULE hDll = ::LoadLibrary(_T("D:/Microsoft/Project/Call_client/debug/client.ocx"));
if (hDll == NULL)
{
std::string str("Load client.ocx Fail !");
result = jenv->NewStringUTF(str.c_str());
return result;

}
FREG lpfunc = (FREG) ::GetProcAddress(hDll, "DllRegisterServer");
if (lpfunc == NULL)
{
std::string str("No Function Found !");
result = jenv->NewStringUTF(str.c_str());
return result;
}
HRESULT hr = lpfunc();
if (hr!=S_OK)
{
std::string str("Get HRESULT :hr !");
result = jenv->NewStringUTF(str.c_str());
}
//初始化
CoInitialize(NULL);
到这里就不知道该怎么初始化这个ocx对象了,于是就用下面的方法(就是被包围的地方,在mfc下面可用,dll下面不能用)
////////////////////////////////////////////////////////
//CRect rect;
//CDclient* cd = new CDclient();
//cd->Create(_T(""), NULL, rect, NULL, 0);
///////////////////////////////////////////////////////

const char* tp = jenv->GetStringUTFChars(jstr,0);
CString key(tp);
这里调用了client.ocx中的方法
VARIANT vrs = cd->GetRestPoint(key);
//////////////////////////////////////////////////////////////////////////
这后面的先不管
VARIANT vars[2];
vars[0].vt =VT_BSTR;
vars[1].vt =VT_BSTR;
long index = 0;
HRESULT hr = ::SafeArrayGetElement(vrs.parray,&index,&vars[index]);
index = 1;
HRESULT hr1 = ::SafeArrayGetElement(vrs.parray,&index,&vars[index]);
BSTR s = vars[0].bstrVal;
BSTR s1 = vars[1].bstrVal;
LPCWSTR lpw = (LPCWSTR)s1;
int size = WideCharToMultiByte(CP_ACP,0,lpw,-1,0,0,NULL,NULL);
//////////////////////////////////////////////////////////////////////////
return result;
}

导入client.ocx后的.h文件
// CDclient.h  : Declaration of ActiveX Control wrapper class(es) created by Microsoft Visual C++
#pragma once
/////////////////////////////////////////////////////////////////////////////
// CDclient
class CDclient : public CWnd
{
protected:
DECLARE_DYNCREATE(CDclient)
public:
CLSID const& GetClsid()
{
static CLSID const clsid
= { 0xCD6ACDB8, 0x8EE0, 0x4C6A, { 0x83, 0x14, 0x73, 0x2, 0x6B, 0x7E, 0x7A, 0xEB } };
return clsid;
}
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID,
CCreateContext* pContext = NULL)
{
return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID);
}

BOOL Create(LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
UINT nID, CFile* pPersist = NULL, BOOL bStorage = FALSE,
BSTR bstrLicKey = NULL)
{
return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID,
pPersist, bStorage, bstrLicKey);
}

// Attributes
public:
// Operations
VARIANT GetRestPoint(LPCTSTR aUserKey)
{
VARIANT result;
static BYTE parms[] = VTS_BSTR ;
InvokeHelper(0x3, DISPATCH_METHOD, VT_VARIANT, (void*)&result, parms, aUserKey);
return result;
}
。。。。


请大家告诉一下改怎么实例化那个client.ocx的对象吧,还有就是为什么mfc下我那种方法可以实例化,dll中不行,谢谢大家
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Call Next 2012-02-15
  • 打赏
  • 举报
回复
我是用jni调的dll,然后用的dll调ocx,所以你说的用函数传是不可能的,
this如果是mfc是可以用的,我都已经用过了的。
现在是java通过jni间接在调用ocx,这个时候这个父窗体指针应该怎么填啊?
用dll?
怎么取dll?
还是没有办法?
fishion 2012-02-15
  • 打赏
  • 举报
回复
你看看 CreateControl就知道了,父窗口的值不能为NULL
fishion 2012-02-15
  • 打赏
  • 举报
回复
不是填this,你要在你的调用的接口函数里添加一个父窗口的指针,然后传到函数里让你的控件使用
Call Next 2012-02-15
  • 打赏
  • 举报
回复
有理由,我确实把this改成了null,那请问一下
1、用CRect rect;
//CDclient* cd = new CDclient();
//cd->Create(_T(""), NULL, rect, NULL, 0);
这种方法的时候第4个参数,也就是父窗口应该填什么啊?this是不行的。

2、如果不用1那种方法创建

//初始化
CoInitialize(NULL);

这里后应该怎么实例化这个ocx啊?
  • 打赏
  • 举报
回复
AfxEnableControlContainer
fishion 2012-02-14
  • 打赏
  • 举报
回复
这是OCX,是一个控件,控件的创建需要指定一个父窗口,你的dll里没有指定一个父窗口给控件,所以就创建不了

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧