Last active
April 21, 2019 06:07
-
-
Save mojobojo/99605f64eda56d319af316e78db40c6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Clearly the best hello world out there. | |
#define _ATL_ATTRIBUTES 1 | |
#define _WINDLL 1 | |
#include <atlbase.h> | |
#include <atlcom.h> | |
#include <string.h> | |
#include <comdef.h> | |
#include <string> | |
#include <fstream> | |
#include <iostream> | |
[module(name = "HelloWorld")]; | |
[object, uuid("08FA02D2-21F4-11E8-B467-0ED5F89F718B"), library_block] | |
__interface IMessageFactory : IUnknown { | |
// interesting it doesnt allow me to use std::string as a param | |
[id(0)] void InternalPrintToConsole(BSTR *str); | |
}; | |
[coclass, uuid("883F0C9E-21F5-11E8-B467-0ED5F89F718B")] | |
class MessageFactory : public IMessageFactory { | |
private: | |
void InternalPrintToConsole(BSTR *str) { | |
std::wstring wstr; | |
wstr.assign(reinterpret_cast<WCHAR *>(*str)); | |
std::wcout << wstr << std::endl; | |
} | |
public: | |
MessageFactory() { | |
} | |
~MessageFactory() { | |
} | |
void PrintStringToConsole(_bstr_t str) { | |
this->InternalPrintToConsole(&str.GetBSTR()); | |
} | |
}; | |
MessageFactory *CreateMessageFactory() { | |
CComObject<MessageFactory> *msg_fac; | |
if (SUCCEEDED(CComObject<MessageFactory>::CreateInstance(&msg_fac))) { | |
return msg_fac; | |
} | |
std::ifstream fail_msg("Unable to create message factory!"); | |
fail_msg.exceptions(fail_msg.failbit); | |
} | |
int main(int argc, char **argv) { | |
try { | |
if (SUCCEEDED(CoInitialize(NULL))) { | |
MessageFactory *message_factory = CreateMessageFactory(); | |
_bstr_t hello_str("Hello World!"); | |
message_factory->PrintStringToConsole(hello_str); | |
} else { | |
std::ifstream fail_msg("CoInitalize failed!"); | |
fail_msg.exceptions(fail_msg.failbit); | |
} | |
} | |
catch (std::exception &ex) { | |
std::cout << ex.what() << std::endl; | |
} | |
return 0; | |
} |
COM makes me twitch.
Put your VC projcet full, please. On Vc2017 community console app do not compile.
Note that usage of ATL attributes is deprecated and produces warning C4467.
I was doing this in C#.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where is AddRef and Release. This program leaks memory.