Dllimport method calling failed in web form

  • Thread starter Thread starter Billy Zhang
  • Start date Start date
B

Billy Zhang

I have a win32dll which is a dll to send SMS.
I am using Dllimport to call the methods in the dll.
In Winform it works well.
In Web form the method doesn't work and returns false. (I am debugging in
development server).
I am login windows by administrator and also add Network service into
administrators group.

Is there something I missed?

Thanks
Billy Zhang
 
Hello Billy,

This is a common issue when ASP.NET code PInvokes native DLLs. EldarM from
MSFT has a good blog entry for the topic:
https://blogs.msdn.com/eldar/archive/2006/09/22/how-to-call-c-dll-from-asp-net-c-code.aspx

As is quoted in the blog:
http://groups.google.com/group/micr...df6e0ac06ba/3a472105feb10c22#3a472105feb10c22
There are generally two reasons for the error in ASP.NET PInvoke:

1. The DLL cannot be found
The solution is to make sure that the directory containing the DLL is in the
environment variable ¡°PATH¡±.
For more information, please refer to Chris¡¯s comments in the above link.

2. The code does not have sufficient permissions to call unmanaged codes
To verify this point, Billy, would you try this aspx page in your solution,
and tell me the result of the function ¡°CanCallUnmanagedCode¡±?

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Security" %>
<%@ Import Namespace="System.Security.Permissions" %>
<html>
<script runat="server">
private static bool CanCallUnmanagedCode
{
get
{
try
{
new
SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
}
catch(SecurityException)
{
return false;
}
return true;
}
}
void Page_Load(Object sender, EventArgs e)
{
if (CanCallUnmanagedCode)
Label1.Text = "This page can call unmanaged code.";
else
Label1.Text = "This page can NOT call unmanaged code.";
}
</script>
<body>
<form runat=server>
<asp:Label id="Label1" Text="Label Control" runat="server"/>
</form>
</body>
</html>

Regards,
Jialiang Ge ([email protected], remove ¡®online.¡¯)
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I get "This page can call unmanaged code".

Jialiang Ge said:
Hello Billy,

This is a common issue when ASP.NET code PInvokes native DLLs. EldarM from
MSFT has a good blog entry for the topic:
https://blogs.msdn.com/eldar/archive/2006/09/22/how-to-call-c-dll-from-asp-net-c-code.aspx

As is quoted in the blog:
http://groups.google.com/group/micr...df6e0ac06ba/3a472105feb10c22#3a472105feb10c22
There are generally two reasons for the error in ASP.NET PInvoke:

1. The DLL cannot be found
The solution is to make sure that the directory containing the DLL is in the
environment variable ¡°PATH¡±.
For more information, please refer to Chris¡¯s comments in the above link.

2. The code does not have sufficient permissions to call unmanaged codes
To verify this point, Billy, would you try this aspx page in your solution,
and tell me the result of the function ¡°CanCallUnmanagedCode¡±?

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Security" %>
<%@ Import Namespace="System.Security.Permissions" %>
<html>
<script runat="server">
private static bool CanCallUnmanagedCode
{
get
{
try
{
new
SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
}
catch(SecurityException)
{
return false;
}
return true;
}
}
void Page_Load(Object sender, EventArgs e)
{
if (CanCallUnmanagedCode)
Label1.Text = "This page can call unmanaged code.";
else
Label1.Text = "This page can NOT call unmanaged code.";
}
</script>
<body>
<form runat=server>
<asp:Label id="Label1" Text="Label Control" runat="server"/>
</form>
</body>
</html>

Regards,
Jialiang Ge ([email protected], remove ¡®online.¡¯)
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thank you Billy for the quick update.

Now that "the page can call unmanaged code", it is more likely to be an
issue of "The DLL cannot be found". Where is your SMS DLL located? Does the
suggestions in EldarM's blog help?

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
I could call the method in the dll and it just returns false. (By the way
there is another method which is successfully.)

So the dll could be found.
 
Hello Billy,

Sorry for my misunderstanding at the very beginning. I assumed that you
meant the DLL could not be called by ASP.NET when it was said "the method
doesn't work and returns false". I'm sorry, Billy, that I should have
clarified my understanding before giving the suggestions.

If the function returns true when run in a winform, but returns false
inside a ASP.NET page, it's likely to be "impersonation" issue. Does the
DLL function needs to run under a certain user account? If yes, the KB
article:

How to implement impersonation in an ASP.NET application
http://support.microsoft.com/kb/306158

may help you with it. It demonstrates several choices of impersonation with
easy-to-follow example codes. Please have a try and let me know whether it
works for you.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
This dll doesn't need to run under a certain user account.
If it runs in asp.net development server which is running under administrator
 
Hello Billy,

Does the DLL belong to Microsoft or a third party? Is there any product
document or code path that says when it returns false? Billy, would you
please send me a sample project or some sample codes that can reproduce the
issue? These information is very necessary for the trouble-shoot of this
issue. Thank you for your cooperation.

In addition, if the DLL is a third party one, I'd suggest you contact the
product owner. They may have a more effective approach to trouble-shoot the
issue, e.g., by debugging into the DLL, or its source code.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
It belongs to third party. I could not send to you.
If no further help, please close this post.

Any way thank you!
-Billy zhang
 
Hello Billy,

Without the necessary information about the third party DLL, I'm sorry that
I do not have further suggestions. If there's anything else I can help with
you, please don't hesitate to tell me.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
Back
Top