2012-10-24 48 views
0

我需要在VB.NET中使用C++ DLL文件。以下是DLL文件的dumpbin在VB.NET中調用C++ DLL文件

D:\Program Files\Microsoft Visual Studio 10.0\VC>dumpbin /exports d:\dll\myssort.dll 
Microsoft (R) COFF/PE Dumper Version 10.00.30319.01 
Copyright (C) Microsoft Corporation. All rights reserved. 
Dump of file d:\dll\ myssort.dll 
File Type: DLL 
    Section contains the following exports for MySort.dll 
    00000000 characteristics 
    3D3F006E time date stamp Thu Jul 25 01:00:54 2002 
     0.00 version 
      1 ordinal base 
      1 number of functions 
      1 number of names 
    ordinal hint RVA  name 
      1 0 00001000 MySortA7 
    Summary 
     1000 .data 
     1000 .rdata 
     1000 .reloc 
     1000 .rsrc 
     3000 .text 

下面是在Visual Basic 6.0對象瀏覽器中顯示的函數定義。

Function SortA7(udtArray As udtA7Rec, nTotalItems As Long) As Long 
    Member of MySort. MySort 
    Sort the elements of A7-type array 

如何在VB.NET中調用此方法?

我試圖做一個DLL導入:

<DllImport("MySort.dll", _ 
SetLastError:=True, CharSet:=CharSet.Auto)> 
Public Function SortA7(ByVal udtArray As MySort.udtA7Rec(), ByVal nTotalItems As Long) As Long 
End Function 

但它給我一個錯誤:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

+0

Vb.Net應用程序和本機C++ dll不能一起玩。你應該在C++/CLI中編寫一些包裝,以便將它們聯繫在一起,但看看你如何格式化你的問題,我懷疑你沒有資格。 –

+3

+1因爲你不值得因爲作爲一個新手互操作而被低估,並且COM – MarkJ

+0

用整數替換長整數。 –

回答

1

如果在Visual Basic 6.0對象瀏覽器顯示出來,這是一個COM DLL。使用COM Interop。在VB.NET中,轉至項目參考,轉至COM選項卡,並添加DLL文件。

+0

奇怪的是,dumpbin.exe輸出顯示它不是* COM服務器。 –

+0

感謝馬克的迴應。 有一個.tlb文件與此dll關聯。在com選項卡中顯示對該tlb文件的引用。我添加了這個參考。但方法SortA7沒有合作。當我輸入MySort。這種智慧只是一個結構成員。 我試着添加對dll的引用,但它給了我下面的錯誤。 「無法添加對'D:\ MySort.dll'的引用,請確保該文件是可訪問的,並且它是有效的程序集或COM組件。」 – user1770583

+0

@HansPassant也許OP的VB6實際上正在調用OP認爲的一個*不同的* DLL。上面的註釋還表明MySort.DLL不是COM服務器。 @ user檢查VB6對象瀏覽器以查看VB6實際調用的內容 – MarkJ