It is currently Mon Jun 01, 2020 4:43 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Process hangs when calling InitJoystick() from a DLL
PostPosted: Sun Oct 13, 2019 3:42 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
I'm extending support for a game by adding additional gamepad features not currently present with a DLL (this is supported). The problem is that the game hangs when loading the library.

I managed to track the problem down to calling InitJoystick() from a DLL. Note that InitJoystick() does not have to be called for the hang to occur.

DLL:

Code:
ProcedureDLL AttachProcess(Instance)
  ;/ Never run
  If #False
    InitJoystick()
  EndIf
EndProcedure


Test Executable:

Code:
;/ Process hangs here
OpenLibrary(#PB_Any,"initjoystick.dll")

Debug 1


You'll see that the program hangs at the call to OpenLibrary() and never reaches the debug line.

I am testing this on Windows 10 64-bit with PureBasic 5.71 x86.


Top
 Profile  
Reply with quote  
 Post subject: Re: Process hangs when calling InitJoystick() from a DLL
PostPosted: Sun Oct 13, 2019 10:03 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2482
Location: Germany
The joystick library uses DirectX.
As far as I know the DirectX must not run in threads (Windows limitation).

Quote:
Notes about creating DLL's:

- The declaration of arrays, lists or map with Dim, NewList or NewMap must always be done inside the procedure AttachProcess.
- Don't write program code outside procedures. The only exception is the declaration of variables or structures.
- DirectX initialization routines must not be written in the AttachProcess procedure.

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Process hangs when calling InitJoystick() from a DLL
PostPosted: Sun Oct 13, 2019 10:24 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
I think it's related to this:

https://docs.microsoft.com/en-us/window ... ing-issues

Code:
For these reasons, Direct3D is designed so that the methods IDirect3DDevice9::Reset, IDirect3D9::CreateDevice, IDirect3DDevice9::TestCooperativeLevel, or the final Release of IDirect3DDevice9 can only be called from the same thread that handles window messages.


InitJoystick() hides any implementation details which would hold any kind of window binding. I still working my way through the DirectInput interfaces but on the surface, this still doesn't make much sense.

DirectInput8 is part of the DirectX SDK but this article is aimed at IDirect3DDevice9. As long as the window receiving notifications is in the same thread as the the DLL, then this shouldn't be a problem. Maybe PureBasic is running the DLL in a separate thread or something.

It is very possible to run DirectX 9 APIs with threads. I still believe this is a problem with implementation and can be fixed.

I'm currently writing a replacement joystick library in PureBasic using DirectInput8 as an exercise. So I'll know either way and possibly have a solution in the meantime.


Top
 Profile  
Reply with quote  
 Post subject: Re: Process hangs when calling InitJoystick() from a DLL
PostPosted: Tue Oct 22, 2019 6:23 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
I just wanted to mention here that I re-implemented joystick functionality through DirectInput8 and it works just fine when called from a DLL. So while this is a fault of PureBasic's implementation, I'm certain that this particular use case was never tested and may even be decided as unsupported.


Top
 Profile  
Reply with quote  
 Post subject: Re: Process hangs when calling InitJoystick() from a DLL
PostPosted: Tue Oct 22, 2019 7:00 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1273
Location: Germany, Glienicke
strange.
Code:
Define Time = ElapsedMilliseconds()

;/ Process hangs here
Debug OpenLibrary(#PB_Any,"C:\Users\Martin Guttmann\Desktop\JoyDLL.dll")

Debug "Time: "+Str(ElapsedMilliseconds()-Time)

Under x64: no problems:
Quote:
31200736
Time: 0

Under x86: First run:
Quote:
5835160
Time: 24273
Next runs under x86: works fine.
Quote:
33819032
Time: 0


Tested with: Windows 7 64-bit with PureBasic 5.71 x86/x64.

_________________
ImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: Process hangs when calling InitJoystick() from a DLL
PostPosted: Tue Oct 22, 2019 8:47 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
That's interesting. Can you confirm whether it works or hangs for you on Windows 10 somewhere?


Top
 Profile  
Reply with quote  
 Post subject: Re: Process hangs when calling InitJoystick() from a DLL
PostPosted: Tue Oct 22, 2019 9:39 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1238
Location: right here
Mistrel wrote:
I am testing this on Windows 10 64-bit with PureBasic 5.71 x86.

Works (does not hang) on Windows 10 64-bit with PureBasic 5.71 LTS x64.

<edit>
Works with PB x86 on Windows x64 as well.


Top
 Profile  
Reply with quote  
 Post subject: Re: Process hangs when calling InitJoystick() from a DLL
PostPosted: Tue Oct 22, 2019 11:25 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
Do you have a joystick or gamepad connected?


Top
 Profile  
Reply with quote  
 Post subject: Re: Process hangs when calling InitJoystick() from a DLL
PostPosted: Tue Oct 22, 2019 11:47 am 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2482
Location: Germany
What you must not do with DLLS is to assign values to variables in the main code. This leads to the stack error and thus to the crash with Windows 7. With Windows 10 it went without crash, but can lead to errors. I have my experiences.

But what works is a call of procedures without parameters. This then has a stack buffer.

Code:
Procedure MyInit()
;
EndProcedure : MyInit()

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: Process hangs when calling InitJoystick() from a DLL
PostPosted: Tue Oct 22, 2019 12:03 pm 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1238
Location: right here
Mistrel wrote:
Do you have a joystick or gamepad connected?
Nope, don't have any to test with.


Top
 Profile  
Reply with quote  
 Post subject: Re: Process hangs when calling InitJoystick() from a DLL
PostPosted: Tue Dec 17, 2019 12:12 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14064
Location: France
Tested both x86 and x64 and both work. Are you sure it's not an AV issue ? If you don't call InitJoystick(), then no code at all is run in the DLL (just checked) so it should be like an empty dll (which contains all joystick functions indeed so it can be flagged by an AV).


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye