Skip to content

Instantly share code, notes, and snippets.

@ASxa86
Created December 22, 2017 05:25
Show Gist options
  • Save ASxa86/e777d2b4856210ffd28391429267105f to your computer and use it in GitHub Desktop.
Save ASxa86/e777d2b4856210ffd28391429267105f to your computer and use it in GitHub Desktop.
fortran value test
project(fortran_value_test)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_executable(${PROJECT_NAME} main.cpp)
#include <Windows.h>
#include <iostream>
typedef void(*Set)(float&);
typedef float(*Get)(void);
class Wrapper
{
public:
Wrapper()
{
this->handle = reinterpret_cast<HMODULE>(LoadLibrary("fortran_value"));
this->setter = reinterpret_cast<Set>(GetProcAddress(handle, "setvalue"));
this->getter = reinterpret_cast<Get>(GetProcAddress(handle, "getvalue"));
}
~Wrapper()
{
FreeLibrary(this->handle);
}
HMODULE handle{};
Set setter{};
Get getter{};
};
int main()
{
// This will pass if the wrapper code below is commented out.
{
const auto handle = reinterpret_cast<HMODULE>(LoadLibrary("fortran_value"));
const auto set = reinterpret_cast<Set>(GetProcAddress(handle, "setvalue"));
const auto get = reinterpret_cast<Get>(GetProcAddress(handle, "getvalue"));
auto value = 1.0f;
set(value);
if(value == get())
{
std::cout << "PASS!\n";
}
else
{
std::cout << "FAIL!\n";
}
FreeLibrary(handle);
}
// Both tests fails if this is uncommented.
{
auto value = 1.0f;
Wrapper wrapper;
wrapper.setter(value);
if(value == wrapper.getter())
{
std::cout << "PASS!\n";
}
else
{
std::cout << "FAIL!\n";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment