Why does C# call an external DLL directly vs. indirectly in the same app?

S

stevo

What causes C# to generate code to call the same external DLL 2
different ways?

At one point in the app, it calls the entry point like this
(disassembled) and it bombs with a null reference:
00000064 call dword ptr ds:[086F1350h]

At another point in the same app, it calls the exact same entry point
like this and it works:
00000051 call 00A5B073

These are calls to the EXACT SAME external DLL from 2 different
classes in the same app. Both calls in C# have the exact same types
of args.



Details if you want them:

The DLL entry point is declared this way:
public class GeoTools
{
[DllImport(DLLName, EntryPoint="_CopyPic@20", ExactSpelling=true)]
public static extern GTResult CopyPic(int DestFileNum, int SrcFileNum,
int PicNum, int FieldNum, int CopyType);
}

Code excerpts (disassembly):

Excerpt 1 bombs at the CALL below:
if ((LastError = GeoTools.CopyPic(-1, m_nStreamNum, PicNum, 0,
0)) != GTResult.Success)
00000050 mov dword ptr [ebp-14h],esi
00000053 mov ebx,dword ptr [esi+50h]
00000056 push dword ptr [ebp-8]
00000059 push 0
0000005b push 0
0000005d mov edx,ebx
0000005f mov ecx,0FFFFFFFFh
00000064 call dword ptr ds:[086F1350h]
0000006a mov ebx,eax
0000006c mov dword ptr [ebp-10h],ebx
0000006f mov eax,dword ptr [ebp-14h]
00000072 mov edx,dword ptr [ebp-10h]
00000075 mov dword ptr [eax+3Ch],edx
00000078 cmp dword ptr [ebp-10h],1
0000007c je 00000094

Excerpt 2 works perfectly; note how it calls the DLL differently:
if ((LastError = GeoTools.CopyPic(-1, m_nStreamNum, PicNum, 0,
0)) != GTResult.Success)
0000003d mov dword ptr [ebp-14h],esi
00000040 mov ebx,dword ptr [esi+54h]
00000043 push dword ptr [ebp-8]
00000046 push 0
00000048 push 0
0000004a mov edx,ebx
0000004c mov ecx,0FFFFFFFFh
00000051 call 00A5B073
00000056 mov ebx,eax
00000058 mov dword ptr [ebp-10h],ebx
0000005b mov eax,dword ptr [ebp-14h]
0000005e mov edx,dword ptr [ebp-10h]
00000061 mov dword ptr [eax+44h],edx
00000064 cmp dword ptr [ebp-10h],1
00000068 je 00000080
 
D

Daniel O'Connell [C# MVP]

stevo said:
What causes C# to generate code to call the same external DLL 2
different ways?

I'd advise posing this question on microsoft.public.dotnet.framework.clr, as
this isn't a C# issue and you have a better shot at finding someone who
knows enough about the JIT to answer it. The JIT is generating this code for
some reason, and unless you ommited a detail it is almost certainly a bug,
but unless the IL generated differs, I don't think the C# compiler has any
direct relation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top