Skip to content

Instantly share code, notes, and snippets.

@mojobojo
Last active April 21, 2019 06:07
Show Gist options
  • Save mojobojo/99605f64eda56d319af316e78db40c6e to your computer and use it in GitHub Desktop.
Save mojobojo/99605f64eda56d319af316e78db40c6e to your computer and use it in GitHub Desktop.
// 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;
}
@DBJDBJ
Copy link

DBJDBJ commented Apr 21, 2019

I was doing this in C#.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment