Skip to content

Commit

Permalink
Fixed issues preventing Foundation.dll to load on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
macmade committed Sep 5, 2016
1 parent 3230631 commit 4664299
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 129 deletions.
214 changes: 109 additions & 105 deletions OBJCXX-Test/main.cpp
Expand Up @@ -32,139 +32,143 @@

int main( void )
{
NS::AutoreleasePool ap;
OBJCXX::RT::Win32Init();

{
NS::AutoreleasePool ap;

{
NS::Object o;
{
NS::Object o;

std::cout << o << std::endl;
}
std::cout << o << std::endl;
}

{
NS::String o( "hello, world" );
{
NS::String o( "hello, world" );

std::cout << o << std::endl;
std::cout << o.UTF8String() << std::endl;
}
std::cout << o << std::endl;
std::cout << o.UTF8String() << std::endl;
}

{
NS::MutableArray o;
{
NS::MutableArray o;

std::cout << o << std::endl;
std::cout << o << std::endl;

o.addObject( NS::String( "hello, world" ) );
o.addObject( NS::String( "hello, universe" ) );
o.addObject( NS::String( "hello, world" ) );
o.addObject( NS::String( "hello, universe" ) );

std::cout << o << std::endl;
}
std::cout << o << std::endl;
}

{
NS::MutableDictionary o;
{
NS::MutableDictionary o;

std::cout << o << std::endl;
std::cout << o << std::endl;

o.setObjectForKey( NS::String( "hello, world" ), NS::String( "key-1" ) );
o.setObjectForKey( NS::String( "hello, universe" ), NS::String( "key-2" ) );
o.setObjectForKey( NS::String( "hello, world" ), NS::String( "key-1" ) );
o.setObjectForKey( NS::String( "hello, universe" ), NS::String( "key-2" ) );

std::cout << o << std::endl;
}
std::cout << o << std::endl;
}

{
NS::String o1( "hello, world" );
NS::String o2( o1 );
{
NS::String o1( "hello, world" );
NS::String o2( o1 );

std::cout << o1 << std::endl;
std::cout << o1.UTF8String() << std::endl;
std::cout << o1 << std::endl;
std::cout << o1.UTF8String() << std::endl;

std::cout << o2 << std::endl;
std::cout << o2.UTF8String() << std::endl;
std::cout << o2 << std::endl;
std::cout << o2.UTF8String() << std::endl;

std::cout << static_cast< id >( o1 ) << std::endl;
std::cout << static_cast< id >( o2 ) << std::endl;
}
std::cout << static_cast< id >( o1 ) << std::endl;
std::cout << static_cast< id >( o2 ) << std::endl;
}

{
NS::FileManager o1;
NS::FileManager o2;
NS::Error e;

o1 = NS::FileManager::defaultManager();
o2 = NS::FileManager::defaultManager();

std::cout << o1 << std::endl;
std::cout << o2 << std::endl;
std::cout << o1.createFileAtPath( "/tmp/OBJCXX-Test.txt", nullptr, nullptr ) << std::endl;
std::cout << o1.attributesOfItemAtPath( "/tmp/OBJCXX-Test.txt", e ) << std::endl;

if( e == nullptr )
{
std::cout << "No error" << std::endl;
}
else
{
std::cout << "Error: " << e << std::endl;
}

std::cout << o1.attributesOfItemAtPath( "/tmp/OBJCXX-Test-2.txt", e ) << std::endl;

if( e == nullptr )
{
std::cout << "No error" << std::endl;
}
else
{
std::cout << "Error: " << e << std::endl;
}
}
{
NS::FileManager o1;
NS::FileManager o2;
NS::Error e;

o1 = NS::FileManager::defaultManager();
o2 = NS::FileManager::defaultManager();

std::cout << o1 << std::endl;
std::cout << o2 << std::endl;
std::cout << o1.createFileAtPath( "/tmp/OBJCXX-Test.txt", nullptr, nullptr ) << std::endl;
std::cout << o1.attributesOfItemAtPath( "/tmp/OBJCXX-Test.txt", e ) << std::endl;

if( e == nullptr )
{
std::cout << "No error" << std::endl;
}
else
{
std::cout << "Error: " << e << std::endl;
}

std::cout << o1.attributesOfItemAtPath( "/tmp/OBJCXX-Test-2.txt", e ) << std::endl;

if( e == nullptr )
{
std::cout << "No error" << std::endl;
}
else
{
std::cout << "Error: " << e << std::endl;
}
}

{
NS::Lock o;
{
NS::Lock o;

std::cout << o << std::endl;
std::cout << o << std::endl;

o.setName( "test" );
o.setName( "test" );

std::cout << o << std::endl;
std::cout << o << std::endl;

o.lock();
o.lock();

std::cout << o << std::endl;
std::cout << o << std::endl;

o.unlock();
o.unlock();

std::cout << o << std::endl;
}
std::cout << o << std::endl;
}

{
std::cout << NS::Date() << std::endl;
}
{
std::cout << NS::Date() << std::endl;
}

{
NS::Log( "hello, world %i %i", 42, 43 );
}
{
NS::Log( "hello, world %i %i", 42, 43 );
}

{
NS::Data o1{};
NS::Data o2{};
NS::MutableData o3{};
NS::String o4{};

o1 = static_cast< id >( o2 );
o1 = static_cast< id >( o3 );

try
{
o1 = static_cast< id >( o4 );
}
catch( const std::runtime_error & e )
{
std::cout << "Caught exception: " << e.what() << std::endl;
}
}
{
NS::Data o1{};
NS::Data o2{};
NS::MutableData o3{};
NS::String o4{};

o1 = static_cast< id >( o2 );
o1 = static_cast< id >( o3 );

try
{
o1 = static_cast< id >( o4 );
}
catch( const std::runtime_error & e )
{
std::cout << "Caught exception: " << e.what() << std::endl;
}
}

#ifdef _WIN32
getchar();
#endif

#ifdef _WIN32
getchar();
#endif
}

return 0;
}
8 changes: 4 additions & 4 deletions OBJCXX.sln
Expand Up @@ -97,16 +97,16 @@ Global
{2C9F804B-2E46-4C93-A355-2E71223EB8E1}.Release|x86.Build.0 = Release|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Debug|32-bit.ActiveCfg = Release|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Debug|32-bit.Build.0 = Release|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Debug|64-bit.ActiveCfg = Release|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Debug|64-bit.Build.0 = Release|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Debug|64-bit.ActiveCfg = Release|x64
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Debug|64-bit.Build.0 = Release|x64
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Debug|x64.ActiveCfg = Debug|x64
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Debug|x64.Build.0 = Debug|x64
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Debug|x86.ActiveCfg = Debug|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Debug|x86.Build.0 = Debug|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Release|32-bit.ActiveCfg = Release|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Release|32-bit.Build.0 = Release|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Release|64-bit.ActiveCfg = Release|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Release|64-bit.Build.0 = Release|Win32
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Release|64-bit.ActiveCfg = Release|x64
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Release|64-bit.Build.0 = Release|x64
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Release|x64.ActiveCfg = Release|x64
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Release|x64.Build.0 = Release|x64
{136630AE-85C6-4C68-B8B7-9541FB2529C7}.Release|x86.ActiveCfg = Release|Win32
Expand Down
28 changes: 8 additions & 20 deletions OBJCXX/source/RT.cpp
Expand Up @@ -169,6 +169,7 @@ XS::PIMPL::Object< OBJCXX::RT::MessageBase >::IMPL::~IMPL( void )
#ifdef _WIN32

#include <Windows.h>
#include <codecvt>

Class ( * objc_getClass )( const char * ) = nullptr;
Class ( * objc_getMetaClass )( const char * ) = nullptr;
Expand All @@ -194,18 +195,16 @@ Ivar ( * class_getInstanceVariable )( Class, const char * )
ptrdiff_t ( * ivar_getOffset )( Ivar ) = nullptr;
void ( * NSLogv )( id, va_list ) = nullptr;

static LONG inited = 0;
static volatile LONG inited = 0;

void OBJCXX::RT::Win32Init( void )
{
char * common;
std::string apple;
wchar_t * ws;
int n;
HMODULE objc;
HMODULE foundation;
if( InterlockedCompareExchange( &inited, 1, 0 ) == false )

if( InterlockedCompareExchange( &inited, 1, 0 ) != 0 )
{
return;
}
Expand All @@ -215,30 +214,21 @@ void OBJCXX::RT::Win32Init( void )
#else
common = getenv( "COMMONPROGRAMFILES(x86)" );
#endif

if( common == nullptr )
{
common = getenv( "COMMONPROGRAMFILES" );
}

apple = std::string( common ) + "\\Apple\\Apple Application Support";
n = MultiByteToWideChar( CP_UTF8, 0, apple.c_str(), -1, NULL, 0 );
ws = ( wchar_t * )malloc( ( ( size_t )n * sizeof( wchar_t ) ) + sizeof( wchar_t ) );

if( ws == NULL )
{
return;
}
SetDllDirectoryA( apple.c_str() );

MultiByteToWideChar( CP_UTF8, 0, apple.c_str(), -1, ws, n );
objc = LoadLibraryA( ( apple + "\\objc.dll" ).c_str() );
foundation = LoadLibraryA( ( apple + "\\Foundation.dll" ).c_str() );

objc = LoadLibrary( ( std::wstring( ws ) + L"\\objc.dll" ).c_str() );
foundation = LoadLibrary( ( std::wstring( ws ) + L"\\Foundation.dll" ).c_str() );

if( objc == nullptr || foundation == nullptr )
{
free( ws );

return;
}

Expand All @@ -265,8 +255,6 @@ void OBJCXX::RT::Win32Init( void )
class_getInstanceVariable = ( Ivar ( * )( Class, const char * ) )GetProcAddress( objc, "class_getInstanceVariable" );
ivar_getOffset = ( ptrdiff_t ( * )( Ivar ) )GetProcAddress( objc, "ivar_getOffset" );
NSLogv = ( void ( * )( id, va_list ) )GetProcAddress( foundation, "NSLogv" );

free( ws );
}

#endif

0 comments on commit 4664299

Please sign in to comment.