帮我看看这个代码有什么问题?

seawaterblue 2012-12-16 08:14:00
这个是一个arcgis的示例代码,在我开发的机器上运行没有问题,但是注册到别人的机器上时在程序运行完退出的时候出现以下错误:


具体信息如下:
************* 异常文本 **************
System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
在 System.Windows.Forms.UnsafeNativeMethods.IOleInPlaceObject.GetWindow(IntPtr& hwnd)
在 System.Windows.Forms.AxHost.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** 已加载的程序集 **************
mscorlib
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
ContentsView
程序集版本: 1.0.4727.37063
Win32 版本: 1.0.4727.37063
基本代码: file:///C:/ContentsView.DLL
----------------------------------------
ESRI.ArcGIS.ArcMapUI
程序集版本: 9.3.0.1770
Win32 版本: 9.3.0.1770
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/ESRI.ArcGIS.ArcMapUI/9.3.0.1770__8fc3cc631e44ad86/ESRI.ArcGIS.ArcMapUI.dll
----------------------------------------
System.Windows.Forms
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
ESRI.ArcGIS.AxControls
程序集版本: 9.3.0.1770
Win32 版本: 9.3.0.1770
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/ESRI.ArcGIS.AxControls/9.3.0.1770__8fc3cc631e44ad86/ESRI.ArcGIS.AxControls.dll
----------------------------------------
ESRI.ArcGIS.Controls
程序集版本: 9.3.0.1770
Win32 版本: 9.3.0.1770
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/ESRI.ArcGIS.Controls/9.3.0.1770__8fc3cc631e44ad86/ESRI.ArcGIS.Controls.dll
----------------------------------------
Accessibility
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
System.Windows.Forms.resources
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
mscorlib.resources
程序集版本: 2.0.0.0
Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------

************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。

例如:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。

那个示例的源程序如下

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMapUI;

namespace CommandInheritingBaseCommand
{

[Guid("46bd0933-acac-4c33-8669-1255db03ca5c")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("CommandInheritingBaseCommand.ZoomToLayer")]
public sealed class ZoomToLayer : BaseCommand
{
#region COM Registration Function(s)
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryRegistration(registerType);

//
// TODO: Add any COM registration code here
//
}

[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
// Required for ArcGIS Component Category Registrar support
ArcGISCategoryUnregistration(registerType);

//
// TODO: Add any COM unregistration code here
//
}

#region ArcGIS Component Category Registrar generated code
/// <summary>
/// Required method for ArcGIS Component Category registration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryRegistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Register(regKey);

}
/// <summary>
/// Required method for ArcGIS Component Category unregistration -
/// Do not modify the contents of this method with the code editor.
/// </summary>
private static void ArcGISCategoryUnregistration(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
MxCommands.Unregister(regKey);

}

#endregion
#endregion

private IApplication m_application;
public ZoomToLayer()
{
//
// TODO: Define values for the public properties
//
base.m_category = "Developer Samples"; //localizable text
base.m_caption = "Zoom To Layer CSharp"; //localizable text
base.m_message = "Zoom to the extent of the active layer in the TOC"; //localizable text
base.m_toolTip = "Zoom To Layer CSharp"; //localizable text
base.m_name = "DeveloperSamples_ZoomToLayerCSharp"; //unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}

#region Overriden Class Methods

/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;

m_application = hook as IApplication;

//Disable if it is not ArcMap
if (hook is IMxApplication)
base.m_enabled = true;
else
base.m_enabled = false;

// TODO: Add other initialization code
}

/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
IMxDocument mxDocument = GetMxDocument(m_application);
ZoomToLayerInTOC(mxDocument);
}

#endregion

#region "Zoom to Active Layer in TOC"

public void ZoomToLayerInTOC(ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument)
{
if (mxDocument == null)
{
return;
}
ESRI.ArcGIS.Carto.IActiveView activeView = mxDocument.ActiveView;

// Get the TOC
ESRI.ArcGIS.ArcMapUI.IContentsView IContentsView = mxDocument.CurrentContentsView;

// Get the selected layer
System.Object selectedItem = IContentsView.SelectedItem;
if (!(selectedItem is ESRI.ArcGIS.Carto.ILayer))
{
return;
}
ESRI.ArcGIS.Carto.ILayer layer = selectedItem as ESRI.ArcGIS.Carto.ILayer;


// Zoom to the extent of the layer and refresh the map
activeView.Extent = layer.AreaOfInterest;
activeView.Refresh();
}
#endregion

#region "Get MxDocument from ArcMap"

public ESRI.ArcGIS.ArcMapUI.IMxDocument GetMxDocument(ESRI.ArcGIS.Framework.IApplication application)
{

if (application == null)
{
return null;
}
ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = ((ESRI.ArcGIS.ArcMapUI.IMxDocument)(application.Document)); // Explicit Cast

return mxDocument;

}
#endregion

}
}
...全文
276 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
seawaterblue 2012-12-20
  • 打赏
  • 举报
回复
自己顶一下!!!!!!!!!
seawaterblue 2012-12-16
  • 打赏
  • 举报
回复
还有那个提示: 要启用实时(JIT)调试, 该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置 jitDebugging 值。 编译应用程序时还必须启用 调试。 例如: <configuration> <system.windows.forms jitDebugging="true" /> </configuration> 我要怎么才能启动JIT调试呢? 我在machine.config 里面添加了那句之后,VS2008工程就打不开了,该怎么弄呢?
seawaterblue 2012-12-16
  • 打赏
  • 举报
回复
引用 2 楼 devmiao 的回复:
ActiveX控件注册不正确,或者你的dll调用有问题,导致非托管内存分配不当。
我是这样注册的: regasm CommandInheritingBaseCommand.dll /tlb: CommandInheritingBaseCommand.tlb /codebase 因为宿主程序是COM的,只有这样才能注册上。 DLL调用的问题还请指教?
devmiao 2012-12-16
  • 打赏
  • 举报
回复
ActiveX控件注册不正确,或者你的dll调用有问题,导致非托管内存分配不当。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧