4

Hi I have a C Dll which will interact with a cobol application. we want to send the data to the cobol through internet.

so i created an C# DLL which will call the C DLL. its working fine when i do a consoleapp, but when i try to call the same DLL from ASP.NET its giving error message

i dont understand that error

An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

There are some other posts regarding this but those were not matching my context i think im missing permissions for asp.net

this is the declaration in my c# dll for calling c dll

[DllImport(@"C:\CCExpert\haz450cp.dll", EntryPoint = "Methodname", CallingConvention = CallingConvention.Cdecl)]
    unsafe public static extern void Methodname(ref p1, ref p2);

Edit:

Hi i changed the settings according to your suggestions but now im getting a new error

Microsoft Visual Studio C Runtime Library has detected a fatal error in w3wp.exe
Raghuveer
  • 2,630
  • 3
  • 29
  • 59
  • If you can call this .dll with the above declaration from a console application, the same declaration should work fine from ASP.NET. This makes it more likely that you have a 32/64-bit issue with IIS. – xxbbcc Feb 15 '12 at 15:42

2 Answers2

7

If you have a C .dll, it'll be either 32-bit or 64-bit. You must make sure that your site is running in the correct platform for that (that is, your site is running as a matching 32/64-bit site.)

In IIS 7, you can configure the bitness of the application pool by right-clicking the app pool and selecting Advanced Settings. There's a setting called Enable 32-bit applications, you need to set that to True for 32-bit sites and False for 64-bit sites.

You can use Dependency Walker to see if the DLL is 32-bit or 64-bit: use View full paths (looks like C:\ on the toolbar) and if the dependencies of your .dll and:

  • If you have a 32-bit OS, your .dll may be 64-bit. You won't be able to load it.
  • If you have a 64-bit OS:
    • If the dependencies are under System32, it's a 64-bit .dll
    • If the dependencies are under SysWOW64, it's a 32-bit .dll

That tells you how to set the application pool. To set the app pool you can check this link.

Make sure you use Advanced Settings when you open the app pool properties, not basic settings. Also, these settings are not available when you bring up properties for your site - they are only available for the app pool.

xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • i checked that, but i'm unable to find the `Enable 32-bit applications` option in my IIS – Raghuveer Feb 15 '12 at 15:35
  • @RaghuveerGuthikonda: what OS are you using? – xxbbcc Feb 15 '12 at 15:39
  • my OS is win 7 ultimate 64bit – Raghuveer Feb 15 '12 at 15:45
  • @RaghuveerGuthikonda: I udated my answer with information on how to update the app pool - check that and see if it helps. – xxbbcc Feb 15 '12 at 15:51
  • hi i updated the question do you know why the error is coming? – Raghuveer Feb 16 '12 at 05:48
  • @RaghuveerGuthikonda I'd look into the DLL for that error. It's not likely that it has anything to do with the loading problem you had at first. My guess is that the DLL is doing an invalid operation internally (null pointer access for example) and that crashes IIS. – xxbbcc Feb 22 '12 at 06:03
  • Hey that problem was resolved... thanks for your help [link](http://stackoverflow.com/questions/9307560/calling-c-dll-from-asp-net-web-service) – Raghuveer Feb 22 '12 at 06:17
2

The most likely cause of this problem is that you are trying to load a 32 bit DLL into a 64 bit process (or vice versa). To verify or rule this out check and see if the Asp.Net is running in 64 bit mode and if so you need to have a 64 bit version of the C DLL

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • i think i have 32 bit dll i cant change the dll right now can i change asp.net to run under 32 bit – Raghuveer Feb 15 '12 at 15:33
  • @RaghuveerGuthikonda i'm not sure that you can. The best information I found was here http://forums.iis.net/t/1152526.aspx – JaredPar Feb 15 '12 at 15:38