Shared library built with wine crashes when calling export functions containing calls to Windows API's

Questions about Wine on Linux
Locked
Vlad1806
Newbie
Newbie
Posts: 2
Joined: Wed Nov 29, 2023 6:57 am

Shared library built with wine crashes when calling export functions containing calls to Windows API's

Post by Vlad1806 »

Hi everyone,
I have simple windows executable with 2 source files (TestApi.cpp and stdafx.cpp)
and simple windows DLL with 3 source file (dllmain.cpp, libTry.cpp and stdafx.cpp)
DLL has few export functions called from my executable (some using Windows API's, some not).

When I build my DLL as static library (using add_library(Try STATIC ${SOURCE_LIB}), that creates libTry.a file) everything works fine.
But when I build my DLL as shared library (using add_library(Try SHARED ${SOURCE_LIB}), that creates libTry.so file)
any export function that has calls to WINDOWS API's fails
(Unhandled exception: page fault on execute access to 0x0000000000000xxx in 64-bit code)

The shell script I am using:

#!/bin/bash
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug .
make

Below is the CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
# Only generate Debug and Release configuration types.
set(CMAKE_CONFIGURATION_TYPES Debug Release)

# Use folders in the resulting project files.
set_property(GLOBAL PROPERTY OS_FOLDERS ON)

# which compilers to use for C and C++
set(CMAKE_C_COMPILER winegcc)
set(CMAKE_CXX_COMPILER wineg++)
set(CMAKE_RC_COMPILER wrc)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/liba)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Project name
set(EXEC_NAME TryLinux)
project(${EXEC_NAME})

# Use folders in the resulting project files.
set_property(GLOBAL PROPERTY OS_FOLDERS ON)
set(CURRENT_SOURCE_DIR ".")

#linux
IF(LINUX)
include_directories(/usr/include)
include_directories(/opt/wine-devel/include/wine/windows)
link_directories("/opt/wine-devel/lib64/wine/x86_64-unix")
#link_directories("/opt/wine-staging/lib64/wine/x86_64-unix")
add_definitions(-DLINUX)
set(LINK_LIBS kernel32 user32 kernelbase winecrt0 dl)
# windows
ELSE(LINUX)
# set output directories for all builds (Debug, Release, etc.)
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/lib )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/lib )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/bin )
endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
ENDIF(LINUX)

#######################################################################
# Creating dynamic library libTry
set(SOURCE_LIB
libTry/libTry.h
libTry/libTry.cpp
libTry/stdafx.cpp
)
include_directories(/opt/wine-devel/include/wine/windows)
set(LINK_LIBS kernel32 user32 kernelbase winecrt0 dl)

# This works (static library)
#add_library(Try STATIC ${SOURCE_LIB})
# This does not work (shared library) !!!!!
# Any export function that has calls to WINDOWS API's fails
# (Unhandled exception: page fault on execute access to 0x00000000000001a4 in 64-bit code)
add_library(Try SHARED ${SOURCE_LIB})

target_compile_definitions(Try PUBLIC -DDLL)
#target_link_libraries(obj PUBLIC Try)
target_link_libraries(Try PUBLIC kernel32 user32 gdi32 ole32)


################################################################################
# Creates executable file ${EXEC_NAME}
set(SOURCE_EXEC
program/stdafx.cpp
program/TryLinux.h
program/TryLinux.cpp
program/TryLinux.rc
)

add_executable(${EXEC_NAME} WIN32 ${SOURCE_EXEC})
target_include_directories(${EXEC_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(${EXEC_NAME} PUBLIC Try PRIVATE ${LINK_LIBS})

message("${WINE_LIB_PATH}")

#linux
IF(LINUX)
#...
# windows
ELSE(LINUX)
set_property(DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT "${EXEC_NAME}")
ENDIF(LINUX)

Any help will be appreciated
Vlad
Locked