# Static linking issue and DLLMain

**URL:** https://discuss.zetetic.net/t/static-linking-issue-and-dllmain/6878
**Category:** SQLCipher
**Created:** [March 26, 2025, 6:46pm UTC](https://discuss.zetetic.net/t/static-linking-issue-and-dllmain/6878 "2025-03-26T18:46:25Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mtissington](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mtissington](https://discuss.zetetic.net/u/mtissington)
#### Post date: [March 26, 2025, 6:46pm UTC](https://discuss.zetetic.net/t/static-linking-issue-and-dllmain/6878/1 "2025-03-26T18:46:25Z")

</div>

Using Visual Studio 2022 on Windows 11 x64  
Using version 4.7 - I have successfully built a sqlcipher.dll which I can load and run perfectly into another dll.

However I’d like to build and link everything into my main dll.

I have defined SQLCIPHER\_OMIT\_DLLMAIN since my main DLL already has a DLLMain

Everything works until I am exiting and closing the database. Then I get a crash and exception. I’m guessing it’s because sqlcipher\_extra\_shutdown() is not being called, but this is only a guess.

This is the stack trace

```auto
>	ActiveWords.dll!sqlcipher_codec_ctx_free(codec_ctx * * iCtx) Line 111273	C
 	ActiveWords.dll!sqlite3FreeCodecArg(void * pCodecArg) Line 112984	C
 	ActiveWords.dll!sqlite3PagerClose(Pager * pPager, sqlite3 * db) Line 63933	C
 	ActiveWords.dll!sqlite3BtreeClose(Btree * p) Line 76222	C
 	ActiveWords.dll!sqlite3LeaveMutexAndCloseZombie(sqlite3 * db) Line 190860	C
 	ActiveWords.dll!sqlite3Close(sqlite3 * db, int forceZombie) Line 190773	C
 	ActiveWords.dll!sqlite3_close_v2(sqlite3 * db) Line 190817	C
 	ActiveWords.dll!sqlite::database::{ctor}::__l2::<lambda>(sqlite3 * ptr) Line 420	C++
 	[External Code]	
 	ActiveWords.dll!CDatabaseManager::Close() Line 281	C++
 	ActiveWords.dll!CDatabaseManager::~CDatabaseManager() Line 36	C++
 	[External Code]	

```

---

<div class="post-metadata">

### Author: ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)
#### Post date: [March 26, 2025, 9:11pm UTC](https://discuss.zetetic.net/t/static-linking-issue-and-dllmain/6878/2 "2025-03-26T21:11:49Z")

</div>

Hello @mtissington - I don’t believe this would be an issue related to `sqlcipher_extra_shutdown()`. That would normally only be called at the point that the library is released and no longer being used. This should happen after `sqlite3_close_v2()`, so it should be unrelated. The only way I could see there being a problem is if you were calling shutdown before close.

That said, you could certainly call `sqlite3_shutdown()` (which would in turn call `sqlcipher_extra_shutdown()` when you are exiting (e.g. from your own DllMain)

Is there anything special or different about the lifecycle on the that database handle being closed? Is it being shared across multiple CDatabaseManagers or anything?

---

<div class="post-metadata">

### Author: ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)
#### Post date: [April 10, 2025, 7:14pm UTC](https://discuss.zetetic.net/t/static-linking-issue-and-dllmain/6878/3 "2025-04-10T19:14:02Z")

</div>

Hello @mtissington - We have attempted to reproduce this issue with a small standalone project that compiles sqlcipher into a wrapping DLL with `SQLCIPHER_OMIT_DLLMAIN` defined on VS 2022. The wrapper DLL contains logic to open a connection uses it, and then close it. The DLL is then loaded from a separate executable.

We did not run into any issues with the scenario as described, everything seemed to work as intended, and there were no crashes or exceptions.

Are you still seeing this issue? If so, can you attempt to narrow down the problem to a small standalone reproduction?

---

<div class="post-metadata">

### Author: ![mtissington](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mtissington](https://discuss.zetetic.net/u/mtissington)
#### Post date: [April 10, 2025, 7:36pm UTC](https://discuss.zetetic.net/t/static-linking-issue-and-dllmain/6878/4 "2025-04-10T19:36:03Z")

</div>

Thanks for the follow up - I’m still having the issue .. will try to narrow it down.

---

<div class="post-metadata">

### Author: ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)
#### Post date: [April 11, 2025, 3:18pm UTC](https://discuss.zetetic.net/t/static-linking-issue-and-dllmain/6878/5 "2025-04-11T15:18:31Z")

</div>

@mtissington - sounds good, thanks. For completeness, we tested this both by statically linking the wrapping DLL with a separate libsqlite3.lib, and also by compiling the amalgamation into the DLL itself.

Out of curiosity, is your DLL and particularly the `CDatabaseManager` being used across multiple threads? Are you compiled with SQLITE\_THREADSAFE set to 1 or 2? Are you using shared cache?

---

<div class="post-metadata">

### Author: ![mtissington](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@mtissington](https://discuss.zetetic.net/u/mtissington)
#### Post date: [April 12, 2025, 2:07am UTC](https://discuss.zetetic.net/t/static-linking-issue-and-dllmain/6878/6 "2025-04-12T02:07:51Z")

</div>

SQLITE\_THREADSAFE IS SET TO 1 and yes, CDatabaseManager is used across multiple thread. Oh and compiling the amalgamation into the DLL itself.

Just in case here are the defines

HAVE\_STDINT\_H;  
\_CRT\_SECURE\_NO\_DEPRECATE;  
\_CRT\_SECURE\_NO\_WARNINGS;  
\_CRT\_NONSTDC\_NO\_DEPRECATE;  
\_CRT\_NONSTDC\_NO\_WARNINGS;  
SQLITE\_THREADSAFE=1;  
SQLITE\_USE\_URI=1;  
SQLITE\_ENABLE\_COLUMN\_METADATA=1;  
SQLITE\_ENABLE\_STAT4=1;  
SQLITE\_ENABLE\_FTS5=1;  
SQLITE\_ENABLE\_LOAD\_EXTENSION=1;  
SQLITE\_ENABLE\_RTREE=1;  
SQLITE\_SOUNDEX=1;  
SQLITE\_ENABLE\_MEMORY\_MANAGEMENT=1;  
SQLITE\_ENABLE\_API\_ARMOR=1;  
SQLITE\_ENABLE\_DBSTAT\_VTAB=1;  
SQLITE\_ENABLE\_STMTVTAB=1  
SQLITE\_TEMP\_STORE=2;  
SQLITE\_EXTRA\_SHUTDOWN=sqlcipher\_extra\_shutdown;  
SQLITE\_EXTRA\_INIT=sqlcipher\_extra\_init;  
SQLITE\_PLACEHOLDER=1;  
SQLITE\_HAS\_CODEC=1;  
SQLITE\_MATH\_FUNCTIONS=1;  
SQLITE\_ENABLE\_MATH\_FUNCTIONS=1;  
SQLITE\_PERCENTILE\_EXTENSION=1;  
SQLITE\_TOTYPE\_EXTENSION=1;  
SQLITE\_SHA256\_EXTENSION=1;

---

<div class="post-metadata">

### Author: ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)
#### Post date: [April 15, 2025, 1:44am UTC](https://discuss.zetetic.net/t/static-linking-issue-and-dllmain/6878/7 "2025-04-15T01:44:36Z")

</div>

Hello @mtissington - thanks for the list. I don’t see any obvious issues with those defines. Let me know if you can narrow down any further reproduction.
