Error in my application delphi using dll file - c#

I have library code in c# using Z339xLib.dll file
the code is :
#region Assembly Z339xLib.dll, v4.0.30319
// C:\Documents and Settings\Hytham\Desktop\Software Protocol n Image Capture SDK_New\Software Protocol n Image Capture SDK\Z339x_SDK_Sample\Z339x_SDK_Sample\bin\Debug\Z339xLib.dll
#endregion
using System;
using System.Drawing;
namespace Z339xLib
{
public class Z339xLibSdk
{
public Z339xLibSdk();
public bool GetImageAndSaveFile(string portname, string filename, int type);
public Bitmap GetImageByBitmap(string portname);
public string SearchDevice_VCOM();
}
}
I need to convert this code to delphi programing, i create a code in delphi like this:
unit Z339xLib_Sdk;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,Controls,Dialogs,
StdCtrls, ExtCtrls;
Const Z339xLibSdk = 'Z339xLib.dll';
function GetImageAndSaveFile(hport:WideString;ImageDir:WideString;Imagetype:integer): boolean; stdcall;external Z339xLibSdk;
function GetImageByBitmap(hport:WideString): BITMAP; stdcall;external Z339xLibSdk;
function SearchDevice_VCOM : string; stdcall;external Z339xLibSdk;
implementation
end.
when call the function i get error :
the applicaion failed to intialize properly(0xc000007b)
please any help or there is any problem with my syantex code.
and this is explaining of the function:
Function bool GetImageAndSaveFile(String Port_Name, String File_Name, int Format);
Parameters String Port_Name = Virual Com Port Name(ex. COM1…).
Or you can just use “AUTO” as a serial port name, system will automatic search the device.
String File_Name A string that contains the name of the file to which to save this Image.
int Format Image Format
0: png
1:bmp
2:Jpeg
3:Tiff
Return values True: Success
False: error occurred.
thanks for your help

Related

Xamarin Forms: Platform specific code does not show xaml file

I have a native sdk in my Xamarin Forms which does nothing. I have a xaml file behind the platform specific .cs file but the contents are not displayed.
Main file
private async void Video()
{
DependencyService.Get<InterfaceFile>().GetNativeMethod();
}
Interface file
public interface InterfaceFile
{
void GetNativeMethod();
}
Native file:
[assembly: Xamarin.Forms.Dependency(typeof(Demo))]
namespace Demo.Droid
{
public partial class Demo : InterfaceFile
{
public string GetNativeMthod()
{
/// code
}
}
}
The above platform specific code has video implementation using third party native sdks. I have xaml code behind this file.
I want to start this video call in this xaml file, which is not happening. How to bind the xaml file to .cs file?
Edit
I have tried removing the xaml code, and added only .cs file with following code(using Vidyo native sdk):
public void GetNativeMethod()
{
_vidyoConnector = new Connector(viewHandle,Connector.ConnectorViewStyle.ConnectorviewstyleDefault, 16, "warning all#VidyoConnector info#VidyoClient", "", 0);
_vidyoConnector.ShowViewAt(viewHandle, 0, 0, viewWidth, viewHeight);
_vidyoConnector.Connect(host, token, displayName, resourceId, this);
}
With above code, app crashes because frame is not defined here.
Native sdk code for Connector method:
public Connector(IntPtr viewId, ConnectorViewStyle viewStyle, uint remoteParticipants, String logFileFilter, String logFileName, ulong userData){
IntPtr nLogFileFilter = MarshalPtrToUtf8.GetInstance().MarshalManagedToNative(logFileFilter ?? string.Empty);
IntPtr nLogFileName = MarshalPtrToUtf8.GetInstance().MarshalManagedToNative(logFileName ?? string.Empty);
objPtr = VidyoConnectorConstructNative(ref viewId, viewStyle, remoteParticipants, nLogFileFilter, nLogFileName, userData);
Marshal.FreeHGlobal(nLogFileName);
Marshal.FreeHGlobal(nLogFileFilter);
VidyoConnectorSetUserDataNative(objPtr, GCHandle.ToIntPtr(GCHandle.Alloc(this, GCHandleType.Weak)));
}
Documentation reference for vidyo: https://developer.vidyo.io/#/documentation

Linking between c# and c++ in 64 bit machine

I have written a code in c++ and c# . From my c++ code i am calling my c# function through. I have sent just a part of c++ code.
txtPath contains the location of a text file.
C++ code:
CoInitialize(NULL);
IMyClassPtr obj3;
obj3.CreateInstance(__uuidof(Program));
obj3->Validation(txtPath);
CoUninitialize();
Validation() is my c# function.
My c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ABC
{
[ComVisible(true)]
public interface IMyClass
{
void Validation(string txtp);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Program : IMyClass
{
private string replace_string(string text)
{
return text.Replace("\r\a", "");
}
public void Validation(string txtp)
{
string[] textValidate = File.ReadAllLines(txtp);
string textpath = txtp;
//validation starts here
foreach (string line in textValidate)
{
string[] strsplit = line.Split(new string[] { "," }, StringSplitOptions.None);
string task = strsplit[0];
string sign = strsplit[1];
string person = strsplit[2];
string routing = strsplit[3];
if (String.IsNullOrEmpty(task) || String.IsNullOrEmpty(sign) || String.IsNullOrEmpty(person))
{
//if the txt file is invalid
MessageBox.Show("Signature.txt is incomplete or has invalid input!!!");
}
}
}
}
}
I have done all the required setting in c# .C# settings snapshot C# project is a class library. My code was working perfectly in 32 bit machine. I was using the generated tlb in other systems by registering it with regasm.exe.
In 64 bit machine by c++ code is working but when the c# linking code is hit the execution stops without throwing any error. I am using a 64 bit machine and created a new project with the same code. Help Please
Make you're using the correct version of 'regasm.exe' for your target platform (i.e. "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\RegAsm.exe"). Take note of 'Framework64'.
On 64-bit Windows, Microsoft does not support loading a 64-bit DLL into a 32-bit process, or vice-versa. For additional information, please refer to the following resource on MSDN:
http://msdn.microsoft.com/en-us/library/aa384231(VS.85).aspx

Automation Add-ins for MS Excel 2013

I am trying to write a C# automation add-in in Visual Studio 2013. The objective is to be able to call UDFs written in C# from within MS Excel 2013. I have read most of the publicly available materials on the subject and have tried to adapt several simple examples, such as.
Unfortunately, neither of them is written under VS 2013 and MSExcel 2013. Code sample:
using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Globalization;
using Microsoft.Win32;
namespace MyFirstAddIn
{
// Early binding. Doesn't need AutoDual.
[Guid("5E6CD676-553F-481E-9104-4701C4DAB272")]
[ComVisible(true)]
public interface IFinancialFunctions
{
double Bid(string symbol);
double Ask(string symbol);
double[,] BidnAsk(string symbol, string direction = null);
}
[Guid("B9B7A498-6F84-43EB-A50C-6D26B72895DA")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class FinancialFunctions : IFinancialFunctions
{
// Private class members.
private static readonly WebClient webClient = new WebClient();
private const string UrlTemplate = "http://finance.yahoo.com/d/quotes.csv?s={0}&f={1}";
// Private method - data download.
private static double GetDoubleDataFromYahoo(string symbol, string field)
{
string request = string.Format(UrlTemplate, symbol, field);
string rawData = webClient.DownloadString(request);
return double.Parse(rawData.Trim(), CultureInfo.InvariantCulture);
}
// Public "interface" methods.
public double Bid(string symbol)
{
return GetDoubleDataFromYahoo(symbol, "b3");
}
public double Ask(string symbol)
{
return GetDoubleDataFromYahoo(symbol, "b2");
}
public double[,] BidnAsk(string symbol, string direction = null)
{
double bid = GetDoubleDataFromYahoo(symbol, "b3");
double ask = GetDoubleDataFromYahoo(symbol, "b2");
return direction == "v" ? new[,]{{bid}, {ask}} : new[,]{{bid, ask}};
}
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"));
RegistryKey key = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), true);
key.SetValue("",System.Environment.SystemDirectory + #"\mscoree.dll",RegistryValueKind.String);
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), false);
}
private static string GetSubKeyName(Type type, string subKeyName)
{
System.Text.StringBuilder s = new System.Text.StringBuilder();
s.Append(#"CLSID\{");
s.Append(type.GUID.ToString().ToUpper());
s.Append(#"}\");
s.Append(subKeyName);
return s.ToString();
}
}
}
I have made the assembly COM-visible via:
Project->Properties->Application->Assembly Information
and I've also registered COM interop in the "Build" tab.
After building, I can see in the registry that registration was successful and the add-in is registered under the correct GUID. However, when I open Excel and go to Developer->Add-ins->Automation, I cannot see my add-in in the list. I verified that the code I'm posting works with Excel 2010 but for some reason I fail to see my add-in in Excel 2013.
Can anyone help me with this?
OK, I have found the cause of the problem. Surprisingly, it turns out that Visual Studio does not have the capability to automatically register 64bit dlls under the correct tree in the registry. The solution is not to register the project for COM interop and manually add commands to invoke the 64bit version of RegAsm as a post-build event. The full path and name of the dll need to be included, so unfortunately this is not a fully automated solution.

Read text file into Clipboard

after along time of searching via google, I decided to poste my problem here.
First: I am total C# Noob. I am using a Macro Recorder from Jitbit and I have no choice to use a different. The Problem is in the Macro Recorder, it is missing some essential things.
Like reading a text file into a variable and paste this variable via Clipboard :-(
However the good thing is, the tool support "some" type of native C# Code
If I open the C# Command I get this:
public class Program
{
public static void Main()
{
System.Windows.Forms.MessageBox.Show("test");
}
}
And the C# program has to follow also these rules:
=> This Code MUST contain a class named "Program" with a static method "Main"
I already used google and found code that should do the job but I get errors, I guess the
code doesn`t follow the above rules.
This is what I found and tried:
using System;
using System.IO;
public class Program
{
public static void Main()
{
// Read the file as one string.
System.IO.StreamReader myFile =
new System.IO.StreamReader("Counter.txt");
string counter = myFile.ReadToEnd();
myFile.Close();
// Load string into clipboard
Clipboard.SetDataObject( counter, true );
}
}
I always get the error : "Line 15: The Name Clipboard is not existing in the context"?!?
I hope that someone can explain a noob (me) what is wrong and what is the correct code.
Thanks.
add reference to System.Windows.Forms
using System;
using System.IO;
using System.Windows.Forms;
public class Program
{
[STAThread]
public static void Main()
{
Clipboard.SetDataObject(File.ReadAllText("Counter.txt"), true);
}
}
Note that to Avoid the ThreadStateException you need to applying the STAThread attribute to your Main() function

Ice Chat Application

I'm ICE starter. At http://zeroc.com there is good tutorial on how to create chat. I decided to use the tutorial as base. And first thing I tried to do was writing ChatRoom class in c# instead of given c++ implementation. I tried to do the same in my c# code. ChatRoom implementation in c++:
// C++
class ChatRoomCallbackAdapter { /* ... */ };
typedef IceUtil::Handle<ChatRoomCallbackAdapter> ChatRoomCallbackAdapterPtr;
class ChatRoom : public IceUtil::Shared
{
public:
ChatRoom(bool trace, const Ice::LoggerPtr& logger);
void reserve(const std::string&);
void unreserve(const std::string&);
void join(const std::string&, const ChatRoomCallbackAdapterPtr&);
void leave(const std::string&);
Ice::Long send(const std::string&, const std::string&);
private:
typedef std::map<std::string, ChatRoomCallbackAdapterPtr> ChatRoomCallbackMap;
ChatRoomCallbackMap _members;
std::set<std::string> _reserved;
IceUtil::Mutex _mutex;
const bool _trace;
const Ice::LoggerPtr _logger;
};
Some piece of class-members implementation:
// ...
void ChatRoom::reserve(const string& name)
{
IceUtil::Mutex::Lock sync(_mutex);
if(_reserved.find(name) != _reserved.end() ||
_members.find(name) != _members.end())
{
throw string("The name " + name + " is already in use.");
}
_reserved.insert(name);
}
// ...
I was writng next:
public class ChatRoom : IceUtil
when I encountered an error. I found that IceUtil dll in distribution package isn't COM-visible therefore I can't use it in my c# project.
What can I use instead of c++
IceUtil::Handle<T>
as far as I understand it is a smart pointer.
How can I implement server like the one's given in c#?
Would it be the same in c# (talking about mutexes) comparing to above c++ class:
public class ChatRoom
{
// ...
void Reserve(System.String Name)
{
lock(this)
{
// operations
}
}
}
?
Thanks!
I don't know anything about ICE, but their website lists a .NET implementation - why don't you use that instead of COM if you want to use C#? There's even a section of documentation with an example of a C# server.
C++ does not support reference counted pointers out of the box, that is why C++ API has IceUtil::Handle<> template. C# obviously does not need it. I'd recommend you start learning Ice for C# using C# examples rather than C++. You can find a lot of C# client/server examples in democs folder of demos packages. And, of course, Ice has absolutely nothing to do with COM technology, except that it is kind of a replacement.

Categories

Resources