修改C#互操作DLL实现PB2017与外部.NET4.6.1 DLL交互的问题
Hey there, let's work through the common snags and fixes you're likely hitting now that you've added the external .NET 4.6.1 DLL to your my.DLL middle layer for PowerBuilder 2017. Since you already had the COM interop working smoothly without the external dependency, we can focus on the specific tweaks needed to get the full chain (PB <-> my.DLL <-> external.DLL) running.
1. Make Sure my.DLL Can Find external.DLL at Runtime
This is the most common pitfall when adding a new dependency. my.DLL needs to locate external.DLL whenever it's called by PB:
- If external.DLL isn't in the GAC:
- Either copy it to the same folder as my.DLL (and also the PB application's runtime directory—since PB will load my.DLL from there when running the compiled EXE)
- Or, in your my.DLL project, set the
Copy Localproperty for the external.DLL reference toTrue—this automatically copies it to your output folder when you build my.DLL.
- If external.DLL is signed with a strong name, you can install it to the GAC with
gacutil /i external.DLL—just make sure it uses the same strong naming setup as my.DLL.
2. Double-Check COM Interop Configuration for my.DLL
Even though you had COM visibility enabled before, adding the external dependency means verifying a few key settings:
- Ensure all public classes/methods in my.DLL that interact with external.DLL are still marked COM-visible. You can do this either via the
[ComVisible(true)]attribute on individual classes/methods, or by keeping the assembly-level COM visibility enabled in your project properties. - Don't expose external.DLL's types directly to COM. PowerBuilder often struggles with non-COM-visible or complex .NET types. Instead, create wrapper methods in my.DLL that take simple PB-friendly types (strings, integers, dates) and handle mapping them to external.DLL's internal types behind the scenes.
- Rebuild my.DLL with the "Register for COM Interop" option enabled—this regenerates the type library (.tlb) that PB uses to recognize your methods.
3. Re-Register my.DLL Properly
After rebuilding my.DLL with the new dependency, you need to refresh its COM registration:
- First, unregister the old version to avoid conflicts:
regasm /u my.DLL - Then register the new build, making sure to generate an updated type library:
regasm my.DLL /tlb:my.tlb(the/tlbflag is critical here—PB needs this file to pick up changes to your interface) - If you installed my.DLL to the GAC, update it there too: run
gacutil /u my.DLLto remove the old version, thengacutil /i my.DLLto add the new one. - Pro tip: Run all these commands in an elevated Command Prompt (right-click > Run as Administrator) to avoid permission errors during registration.
4. PowerBuilder-Specific Checks
Now let's ensure PB can still communicate with the updated my.DLL:
- In the PB IDE, refresh your COM reference: Remove the old my.DLL reference, then re-add it by browsing to the new
my.tlbfile. This ensures PB picks up any new methods or interface changes. - When calling my.DLL methods from PB, stick to simple data types. Avoid passing PB-specific objects directly—instead, have my.DLL handle creating and managing external.DLL's complex types internally, using basic parameters from PB to trigger those operations.
- Test both in the PB IDE and as a compiled EXE. The IDE's working directory might be different from the EXE's runtime folder, so dependencies might load in one but not the other.
5. Debugging Tips to Pinpoint Exact Issues
If you're still getting errors (like "Class not registered" or "Could not load file or assembly"), here's how to dig deeper:
- Use the Fusion Log Viewer (fuslogvw.exe) to track assembly loading. It will show you exactly where the .NET runtime is looking for external.DLL and what's causing the load failure.
- Attach Visual Studio to the PB IDE process (pb170.exe) and set breakpoints in my.DLL's methods. This lets you step through the code to see if calls are reaching external.DLL, and where errors occur.
- Check the Windows Event Viewer for COM-related errors—sometimes it provides more detailed context about registration or loading issues that aren't visible in PB's error messages.
内容的提问来源于stack exchange,提问作者drewglew

