Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1096765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:19:44+00:00 2026-05-17T00:19:44+00:00

I try to load a simple DLL compiled with GCC in cygwin into a

  • 0

I try to load a simple DLL compiled with GCC in cygwin into a C#.NET application. The DLL looks like this

#ifndef __FOO_H
#define __FOO_H

#if _WIN32
  #define EXPORT extern "C" __declspec(dllexport)
#else //__GNUC__ >= 4
  #define EXPORT extern "C" __attribute__((visibility("default")))
#endif

EXPORT int bar();

#endif  // __FOO_H

The function bar() just returns 42.

I compiled and linked the DLL with

g++ -shared -o foo.dll foo.cpp

Now I want to load this super simple DLL into a C# WinForms application.

public partial class Form1 : Form
{
  [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
  static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

  [DllImport("kernel32", SetLastError = true)]
  static extern IntPtr LoadLibrary(string lpFileName);

  public delegate IntPtr Action2();

  unsafe public Form1()
  {
    InitializeComponent();

    IntPtr pcygwin = LoadLibrary("cygwin1.dll");
    IntPtr pcyginit = GetProcAddress(pcygwin, "cygwin_dll_init");
    Action init = (Action)Marshal.GetDelegateForFunctionPointer(pcyginit, typeof(Action));
    init();
  }

  unsafe private void button1_Click(object sender, EventArgs e)
  {
    IntPtr foo = LoadLibrary("foo.dll");         // CRASH ... sometimes
    IntPtr barProc = GetProcAddress(foo, "bar");
    Action2 barAction = (Action2)Marshal.GetDelegateForFunctionPointer(barProc, typeof(Action2));
    IntPtr inst = barAction();
  }
}

Now the strange thing is: sometimes it works and sometimes it doesn’t. When it doesn’t work it crashes when it loads foo.dll. I run it in debug mode but I don’t even get an exception. The debugger just stops as if I stopped it myself!

I also tried to load foo.dll in the same stack frame where I load cygwin1.dll. Same thing!

Any hints why this happens and what I can do to make it work?

Update 1: We use the latest cygwin and Visual Studio 2010.

Update 2: An assumption is that it has to with timing and garbage collection. It seems to me that the time between loading cygwin1.dll and loading foo.dll matters. The shorter the time between the two LoadLibrary calls the more likely it seems to work.

Update 3: If loading foo.dll succeeds the first time it succeeds always during a session. I can click button1 as often as I want.

Note: LoadLibrary(“foo.dll”) does not simply fail to load foo.dll. That’d be nice. I crashes and the debugger stops working. Not even an exception is thrown. AND it does not crash always. Sometimes it works!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:19:44+00:00Added an answer on May 17, 2026 at 12:19 am

    Look at the “UPDATED” part of my old answer about the close problem. I recommend you to to compile your DLL with respect of MinGW tools instead of CygWin tools. If nothing is changed in the since the time the requirement “Make sure you have 4K of scratch space at the bottom of your stack” makes CygWin DLLs incompatible to .NET. I don’t know how to realized the requirement in a .NET application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using xml.net in web application When I try load xml through an
I try to add an addons system to my Windows.Net application using Reflection; but
When I try to load a solution in Visual Studio 2008, it immediately closes.
I currently have issues in Webkit(Safari and Chrome) were I try to load dynamically
I am assigning ViewState[something] in Page Load of content page and when I try
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
try { ... } catch (SQLException sqle) { String theError = (sqle).getSQLState(); ... }
I try to fetch a Wikipedia article with Python's urllib: f = urllib.urlopen(http://en.wikipedia.org/w/index.php?title=Albert_Einstein&printable=yes) s
I try to define a schema for XML documents I receive. The documents look
We try to convert from string to Byte[] using the following Java code: String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.