|
Hi guys,
I have two vectors of string
and I'm looking for an easy way to find the first vector sequence or sub-sequence into the first one
for example:
Vect_1 < "JAVA", "ADA", "C", "C++", "C#">
Vect_2 < "C++", "C#">
search vect_2 sequence in vect_1 will return vect_1 position 3 and match length 2
Vect_1 < "JAVA", "ADA", "C", "C++", "C#">
Vect_2 < "JAVA", "C#">
search vect_2 sequence in vect_1 will return vect_1 position 0 and match length 1
thanks for any idea, suggestion
|
|
|
|
|
The simplest algorithm is probably this:
std::pair<size_t, size_t> find_first_subsequence(std::vector<std::string> const& v1,
std::vector<std::string> const& v2)
{
std::vector<std::string>::const_iterator pos = std::find(v1.begin(), v1.end(), v2[0]);
const size_t firstPos = pos-v1.begin();
if (pos == v1.end()) return std::make_pair(-1, -1);
size_t numEqual = 0;
while(numEqual < v2.size() && pos != v1.end() && *pos == v2[numEqual])
{
++numEqual;
++pos;
}
return std::make_pair(firstPos, numEqual);
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
|
Because std::search only returns a match if it finds the whole of a sequence you're looking for, not a partial match, which is what the OP was looking for.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Fair point. I didn't read the OP's post carefully enough.
Steve
|
|
|
|
|
I'm at the end of my rope here... I've been bashing my head on my keyboard for the last week trying to figure out this issue.
I'm trying to display a flash control on a DirectDraw surface and my call to AtlAxAttachControl fails - preventing the flash player from appearing within the control.
Here's a stripped down version of my code:
#pragma once
#include <string>
#include <windows.h>
#include <exdisp.h>
#include <mshtmlc.h>
#include <atlbase.h>
#include <atlcom.h>
#include <atlwin.h>
using std::string;
#import "PROGID:ShockwaveFlash.ShockwaveFlash" no_namespace raw_interfaces_only
typedef HRESULT (WINAPI *LPAtlAxWinInit) ();
typedef HRESULT (WINAPI *LPAtlAxGetControl)(HWND hwnd, IUnknown** unk);
class FlashViewer
{
public:
FlashViewer();
~FlashViewer();
bool Init(int Width, int Height);
void OpenFlash(const char* filename);
void DrawToSurface(LPDIRECTDRAWSURFACE7 lpdds);
private:
int mViewerWidth;
int mViewerHeight;
HWND mViewerWnd;
IShockwaveFlash* mFlashCtrl;
};
FlashViewer::FlashViewer()
{
mViewerWidth = 0;
mViewerHeight = 0;
mViewerWnd = 0;
mFlashCtrl = NULL;
}
FlashViewer::~FlashViewer()
{
DestroyWindow(this->mViewerWnd);
if (this->mFlashCtrl != NULL)
{
this->mFlashCtrl->Release();
this->mFlashCtrl = NULL;
}
}
bool FlashViewer::Init(int width, int height)
{
LPAtlAxWinInit AtlAxWinInit3 = (LPAtlAxWinInit)GetProcAddress(LoadLibrary("atl"), "AtlAxWinInit");
LPAtlAxGetControl AtlAxGetControl3 = (LPAtlAxGetControl)GetProcAddress(LoadLibrary("atl"), "AtlAxGetControl");
MSG msg;
HRESULT hr = AtlAxWinInit3();
HWND hwnd = CreateWindow("AtlAxWin", "", WS_VISIBLE|WS_POPUP, 0, 0, 1024, 768, 0, 0, 0, 0);
IShockwaveFlash* flash = 0;
hr = CoCreateInstance(__uuidof(ShockwaveFlash), 0, CLSCTX_ALL, __uuidof(IShockwaveFlash), (void **)&flash);
hr = flash->put_WMode(L"transparent");
hr = flash->put_Loop(true);
hr = AtlAxAttachControl(flash, hwnd, NULL);
hr = flash->put_Movie(L"c:\\FrontEnd.swf");
long pVal = -1;
flash->get_ReadyState(&pVal);
return true;
}
void FlashViewer::OpenFlash(const char *filename)
{
this->mFlashCtrl->LoadMovie(0, _bstr_t(filename));
}
void FlashViewer::DrawToSurface(LPDIRECTDRAWSURFACE7 lpdds)
{
if (this->mViewerWnd == NULL)
return;
RECT rect = {0, 0, this->mViewerWidth, this->mViewerHeight};
HDC hdcSurface;
HRESULT hr = lpdds->GetDC(&hdcSurface);
if (FAILED(hr))
return;
SetMapMode(hdcSurface, MM_TEXT);
OleDraw(this->mFlashCtrl, DVASPECT_CONTENT, hdcSurface, &rect);
lpdds->ReleaseDC(hdcSurface);
}
When debugging - the console displays two First-chance exceptions as soon as AtlAxAttachControl() is called... Both are 0x8001010D: An outgiong call cannot be made since the application is dispatching an input-syncronous call.
This problem is totally driving me crazy... PLEASE SOMEONE HELP.
|
|
|
|
|
I suspect that it's a COM threading issue. Do you have a single threaded app or maybe a single-threaded apartment model app? Is FlashViewer::Init called from within a COM method?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Its a single threaded app but I think you might have something on the apartment model. I'm using this as a control inside a WIN32 window. The odd thing is that the FlashViewer::Init still throws the same error even if its executed before the WIN32 window is declared. The other strange thing is that this code executes if its included on its own like this:
#include <string>
#include <windows.h>
#include <exdisp.h>
#include <mshtmlc.h>
#include <atlbase.h>
#include <atlcom.h>
#include <atlwin.h>
using std::string;
#import "PROGID:ShockwaveFlash.ShockwaveFlash" no_namespace raw_interfaces_only
typedef HRESULT (WINAPI *LPAtlAxWinInit) ();
typedef HRESULT (WINAPI *LPAtlAxGetControl)(HWND hwnd, IUnknown** unk);
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd, int show)
{
LPAtlAxWinInit AtlAxWinInit3 = (LPAtlAxWinInit)GetProcAddress(LoadLibrary("atl"), "AtlAxWinInit");
LPAtlAxGetControl AtlAxGetControl3 = (LPAtlAxGetControl)GetProcAddress(LoadLibrary("atl"), "AtlAxGetControl");
MSG msg;
HRESULT hr = AtlAxWinInit3();
HWND hwnd = CreateWindow("AtlAxWin", "", WS_VISIBLE|WS_POPUP, 0, 0, 1024, 768, 0, 0, 0, 0);
IShockwaveFlash* flash = 0;
IViewObject* view = 0;
hr = CoCreateInstance(__uuidof(ShockwaveFlash), 0, CLSCTX_ALL, __uuidof(IShockwaveFlash), (void **)&flash);
hr = flash->put_WMode(L"transparent");
hr = flash->put_Loop(true);
hr = AtlAxAttachControl(flash, hwnd, NULL);
hr = flash->put_Movie(L"c:\\FrontEnd.swf");
hr = flash->QueryInterface(__uuidof(IViewObject), (void **) &view);
long pVal = -1;
flash->get_ReadyState(&pVal);
BSTR val = L"Null", var = L"GetLastButton";
string cval;
while(GetMessage(&msg, 0, 0, 0) && flash)
{
hr = flash->CallFunction(L"<invoke name=\"GetLastButton\" returntype=\"string\"></invoke>" , &val);
cval = _com_util::ConvertBSTRToString(val);
if (cval.compare("<string>btnQuit</string>") == 0 )
{
PostMessage(hwnd, WM_QUIT, NULL, NULL);
}
DispatchMessage(&msg);
}
};
I tried using OleInitialize(NULL); at the start of the INIT function which is supposed to call CoInitializeEx with COINIT_APARTMENTTHREADED. But I still got the same problem.
Heres my shell for the windows app (excuse the amount of code):
WIN32APP.H
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma once
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define COLOR_DEPTH 32
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
class Win32App
{
public:
Win32App() { };
~Win32App() { };
void Start(HINSTANCE hInst, bool isFullscreen);
char const * Title;
char const * ClassName;
bool isFullscreen;
static HINSTANCE hInst;
static HWND hWnd;
private:
WNDCLASS localWndClass;
static LRESULT CALLBACK WindowsProcedure(HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam);
protected:
virtual void PreInitialize() { };
virtual void PostInitialize() { };
virtual void Main() { };
virtual void CleanUp() { };
void StartMessagePump();
void Quit()
{
PostMessage(hWnd, WM_QUIT, 0, 0);
}
};
HINSTANCE Win32App::hInst;
HWND Win32App::hWnd;
void Win32App::Start(HINSTANCE hInst, bool isFullscreen)
{
this->hInst = hInst;
this->Title = "Win32App";
this->ClassName = "Win32";
this->isFullscreen = isFullscreen;
this->PreInitialize();
localWndClass.style = 0;
localWndClass.lpfnWndProc = WindowsProcedure;
localWndClass.cbClsExtra = 0;
localWndClass.cbWndExtra = 0;
localWndClass.hInstance = this->hInst;
localWndClass.hIcon = 0;
localWndClass.hCursor = LoadCursor(0, IDC_ARROW);
localWndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
localWndClass.lpszMenuName = 0;
localWndClass.lpszClassName = ClassName;
RegisterClass(&localWndClass);
if (!isFullscreen)
this->hWnd = CreateWindow(
this->ClassName,
this->Title,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
NULL);
else
this->hWnd = CreateWindow(
this->ClassName,
this->Title,
WS_EX_TOPMOST | WS_POPUP,
0,
0,
SCREEN_WIDTH,
SCREEN_HEIGHT,
NULL,
NULL,
hInst,
NULL);
ShowWindow(this->hWnd, 1);
UpdateWindow(this->hWnd);
this->PostInitialize();
StartMessagePump();
}
LRESULT CALLBACK Win32App::WindowsProcedure(HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
void Win32App::StartMessagePump()
{
MSG msg;
DWORD startTime;
while (true)
{
startTime = GetTickCount();
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(KEY_DOWN(VK_ESCAPE))
PostMessage(hWnd, WM_QUIT, 0, 0);
this->Main();
}
this->CleanUp();
this->~Win32App();
}
DDAPP.H
#pragma once
#pragma comment (lib, "ddraw.lib")
#pragma comment (lib, "dxguid.lib")
#include <ddraw.h>
#include "Win32App.h"
#define INIT_DXSTRUCT(dxs) {ZeroMemory(&dxs, sizeof(dxs)); dxs.dwSize = sizeof(dxs);}
class DDApp : public Win32App
{
public:
DDApp()
{
}
static void LoadBitmap(LPDIRECTDRAWSURFACE7 lpdds, int xDest, int yDest, LPCSTR Filename);
void Restore();
protected:
virtual void DDInitialize() {}
virtual void Update() {}
virtual void RenderFrame() {}
virtual void Release() {}
virtual void PostInitialize();
virtual void Main();
virtual void CleanUp();
LPDIRECTDRAW7 lpDD7;
LPDIRECTDRAWSURFACE7 lpddsPrimary;
LPDIRECTDRAWSURFACE7 lpddsBack;
DDSURFACEDESC2 ddsd;
};
void DDApp::PostInitialize()
{
CoInitialize(NULL);
CoCreateInstance(CLSID_DirectDraw, NULL, CLSCTX_ALL, IID_IDirectDraw7, (void**)&lpDD7);
lpDD7->Initialize(NULL);
if (isFullscreen)
lpDD7->SetCooperativeLevel(hWnd, DDSCL_ALLOWREBOOT | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
else
lpDD7->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
lpDD7->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, COLOR_DEPTH, 0, 0);
lpddsPrimary = NULL;
lpddsBack = NULL;
INIT_DXSTRUCT(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.dwBackBufferCount = 1;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
DDSCAPS_COMPLEX |
DDSCAPS_FLIP |
DDSCAPS_VIDEOMEMORY;
lpDD7->CreateSurface(&ddsd, &lpddsPrimary, NULL);
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
lpddsPrimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsBack);
DDInitialize();
}
void DDApp::Main()
{
Update();
RenderFrame();
HRESULT r = lpddsPrimary->Flip(NULL, DDFLIP_WAIT);
}
void DDApp::CleanUp()
{
Release();
lpddsBack->Release();
lpddsPrimary->Release();
lpDD7->Release();
}
void DDApp::LoadBitmap(LPDIRECTDRAWSURFACE7 lpdds, int xDest, int yDest, LPCSTR Filename)
{
HDC hSrcDC;
HDC hDestDC;
HBITMAP hbitmap;
BITMAP bmp;
int nHeight, nWidth;
hbitmap = (HBITMAP)LoadImage(hInst, Filename, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
hSrcDC = CreateCompatibleDC(NULL);
SelectObject(hSrcDC, hbitmap);
GetObject(hbitmap, sizeof(BITMAP), &bmp);
nWidth = bmp.bmWidth;
nHeight = bmp.bmHeight;
lpdds->GetDC(&hDestDC);
BOOL isSuccess = BitBlt(hDestDC, xDest, yDest, nWidth, nHeight, hSrcDC, 0, 0, SRCCOPY);
lpdds->ReleaseDC(hDestDC);
}
void DDApp::Restore()
{
lpddsPrimary->Restore();
lpddsBack->Restore();
}
FlashViewer::Init() is called from DDInitialize() Function which is called from the PostInitialize() Function which is run after the WIN32 windows creation and before the message pump. I put comments in to try and help out.
if (FV.Init(this->hWnd, SCREEN_WIDTH, SCREEN_HEIGHT))
FV.OpenFlash("C:\\FrontEnd.swf");
|
|
|
|
|
My suspicion would still be threading - and something to do with the message loop. I suspect the Flash control uses one or more threads other than the main UI thread. In that situation, Windows uses window messages to transfer control between threads.
Other than that, I can't really think of any specific advice - sorry
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Ahhh yikes. Well if you can think of anything let me know. Is there a way I can get the attach control to run thru the message loop? Like posting a custom message?
|
|
|
|
|
Hi,I think you should add:
<br />
CComModule _Module;
<br />
CoInitialize(0); <br />
_Module.Init(0,GetModuleHandle(NULL)); <br />
AtlAxWinInit(); <br />
before you call AtlAxAttachControl() function.
|
|
|
|
|
Hi everyone:
Just like the subject, thanks.
|
|
|
|
|
If it's a non-template class and you export it from the DLL[^], sure, why not?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
OK, thanks for your reply, I have fixed the problem, the exe need to choose link to ATL.
|
|
|
|
|
Hi,
I have a COM server (not DCOM) and would like to only accept calls from one known client. Is there anyway that the COM server can find out the caller's information and determine if the client is trusted?
I've seen the documents about the security features, mostly for DCOM, that usually involve user authentication. But in my case, both client and server runs on the local machine.
Thanks,
|
|
|
|
|
IServerSecurity [^]?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
hmm, that is not exactly what I need. I looks like I can find out the user information, who runs the client. But not the client process itself.
Is there anyway I can find out more client information, for example, the client process id.
|
|
|
|
|
lucyh3h wrote: Is there anyway I can find out more client information, for example, the client process id.
You could get the client to tell you its process id?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
No, I am trying to authenticate the client, so I don't trust whatever it tells me.
I guess this is an issue common to all Local RPC. Does the server always trust the client running on the same machine?
|
|
|
|
|
I suspect the standard COM security API[^]s are the only real way to authenticate the client.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
In normal C++ when a class header only contains a reference or a pointer to a class it is better to not #include the definition but wait until the .cpp file to avoid coupling/avoid recompilation.
In our of our class headers the reference concerned is to a smart pointer generated from a #import, in other words a smart pointer of the form _COM_SMARTPTR_TYPEDEF(IComInterface, &__uuidof(IComInterface))
Which expands to typedef _com_ptr_t<_com_IIID<icominterface,> > IComInterface;
I think the problem is that the __declspec(uuid("GUID VALUE")) which is attached to the forward declaration of the IComInterface struct like __stdcall and __fastcall is part of the declaration and so will produce different types depending on the contents of uuid. The problem however is that the uuid contents will be autogenerated by the #import line, and so it won't be possible know ahead of time to know what to put in the typedef.
I also think that the $(InputName)_i.c file generated when building the COM component won't help because although it contains the correct GUID it won't be in the correct form for __uuidof
|
|
|
|
|
Try declaring a variable that (in some .cpp file) you'll define & initialise to the IID of the interface, like this:
class ITest ;
extern const IID ITest_IID;
_COM_SMARTPTR_TYPEDEF(ITest, ITest_IID);
That correctly forward declares ITestPtr so you can a) use a reference to it without needing the definition of ITest or ITest_IID , but b) can use the thing referred to once ITest and ITest_IID have been defined.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I get the error "redefinition; different basic types" when the #import defines the real thing.
|
|
|
|
|
You need to decouple things a bit more in that case. Here's an example that builds:
In the .h file:
class ITest ;
extern const IID ITest_IID;
_COM_SMARTPTR_TYPEDEF(ITest, ITest_IID);
In the .cpp file:
#import "libid:00020813-0000-0000-C000-000000000046" version("1.6") auto_search no_dual_interfaces rename("DialogBox", "excelDialogBox") rename("RGB", "excelRGB") rename("DocumentProperties", "excelDocumentProperties") rename("SearchPath", "excelSearchPath") rename("CopyFile", "excelCopyFile") rename("ReplaceText", "excelReplaceText")
class ITest : public Excel::_Application {};
const IID ITest_IID = __uuidof(Excel::_Application);
This defines a smart pointer, ITestPtr that wraps an Excel _Application pointer.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
It works up to a point, but the moment I introduce an accessor function to return a valid COM object the type ITest I get error C2027: use of undefined type 'ITest'
So:
HashImport.h
#pragma once
#include <comdef.h>
struct ITest;
extern const IID ITest_IID;
_COM_SMARTPTR_TYPEDEF(ITest, ITest_IID);
class CHashImport
{
public:
CHashImport();
~CHashImport();
HRESULT GetImpl(ITestPtr &pImpl);
};
</comdef.h>
HashImport.cpp
#include "StdAfx.h"
#include "Hashimport.h"
#import "libid:00020813-0000-0000-C000-000000000046" version("1.6") auto_search no_dual_interfaces rename("DialogBox", "excelDialogBox") rename("RGB", "excelRGB") rename("DocumentProperties", "excelDocumentProperties") rename("SearchPath", "excelSearchPath") rename("CopyFile", "excelCopyFile") rename("ReplaceText", "excelReplaceText")
struct ITest : public Excel::_Application {};
using namespace Excel;
const IID ITest_IID = __uuidof(Excel::_Application);
CHashImport::CHashImport()
{
ITestPtr ptr;
GetImpl(ptr);
}
CHashImport::~CHashImport()
{
}
HRESULT CHashImport::GetImpl(ITestPtr &pImpl)
{
pImpl = ITestPtr(__uuidof(Excel::Application));
_bstr_t appname = pImpl->GetValue();
return S_OK;
}
referenceheader.h
#pragma once
#include "hashimport.h"
class CReferenceHeader
{
public:
CReferenceHeader();
~CReferenceHeader();
};
referenceheader.cpp
#include "StdAfx.h"
#include "Referenceheader.h"
CReferenceHeader::CReferenceHeader()
{
}
CReferenceHeader::~CReferenceHeader()
{
}
main.cpp
#include "stdafx.h"
#include "hashimport.h"
#import "libid:00020813-0000-0000-C000-000000000046" version("1.6") auto_search no_dual_interfaces rename("DialogBox", "excelDialogBox") rename("RGB", "excelRGB") rename("DocumentProperties", "excelDocumentProperties") rename("SearchPath", "excelSearchPath") rename("CopyFile", "excelCopyFile") rename("ReplaceText", "excelReplaceText")
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
CHashImport import;
::CoUninitialize();
return 0;
}
|
|
|
|
|